How am i supposed to insert a word into a blank row?

$sql = "INSERT INTO ue-userfile WHERE id='$imageid' AND userid='$userid' (`albumname`) VALUES('$newalbum')";

?

$sql  = "
            INSERT INTO `ue-userfile` (
              ``,  ``, ``, ``, ``, ``,``,`albumname`
            )
            VALUES ('{$albumname}')";

?
God damn annoying to not find any solution on how to insert or update a blank row...

faroukmuhammad commented: You 've solve it yourself, the second option will work. +4

Dani AI

Generated

Quick summary tied to the replies: is trying to change an album name but the query does nothing. pointed at INSERT (for new rows) and pointed at UPDATE (for existing rows). If the record exists you must UPDATE it; if not, INSERT or use an "upsert" strategy such as ON DUPLICATE KEY UPDATE.

Most likely causes and quick checks:

  • A hyphen in the table name (ue-userfile) breaks unquoted identifiers. Either rename the table or quote it with backticks; MySQL treats a bare hyphen as minus. See the identifier rules in the MySQL manual (MySQL identifiers).
  • Make sure both POST keys are truly present (use isset or !empty for each) and that the form field names match.
  • After running the query check for errors and affected rows so you see why nothing changed:
$result = mysql_query($sql);
if (! $result) {
    die('MySQL error ' . mysql_errno() . ': ' . mysql_error());
}
echo mysql_affected_rows() . ' rows affected';

Longer-term fixes:

  • Stop storing HTML-encoded values; use htmlentities() when outputting to HTML, not before storing. Validate/cast the id (e.g. (int)$imageid).
  • Move off old mysql_* functions and use prepared statements (PDO or mysqli) to avoid SQL injection and get better error handling. Example PDO pattern and docs: PDO prepared statements.

If the goal is "insert if missing, otherwise update", consider MySQL's ON DUPLICATE KEY UPDATE feature (INSERT ... ON DUPLICATE KEY UPDATE).

Recommended Answers

All 4 Replies

http://dev.mysql.com/doc/refman/5.5/en/insert.html

$sql = "INSERT INTO ue-userfile (`id`, `userid`, `albumname`) VALUES ('$imageid', '$userid', '$newalbum')";

i only need one inserted.

mysql_query("UPDATE ue-userfile SET userid = 'The userid' WHERE id = 'the id'");

Is that what you wanted?

if(isset($_POST['albumname']) && $_POST['imageid']){

	$newalbum = $_POST['albumname'];
	$newalbum = htmlentities($newalbum);
	
	$imageid = $_POST['imageid'];

	$imageid = htmlentities($imageid);
	$sql5 = "UPDATE ue-userfile SET albumname = '$newalbum' WHERE id = '$imageid'";
	$result5 = mysql_query($sql5);}

i've checked my connection to the database, also the outputs of the variables, its all fine. but it wont change the current album name to the new one $newalbum

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.