Jump to content

Why doesn't separating the dollar sign work


jasonkirk

Recommended Posts

Setup a array that held all the query info for a database with the plan to put the second of code that updates the database into a function.  But hasn't been working.  In the end I widdled it down to the question, why don't these two bits of code work the same?
 
Array:
 
$database_info = array("db_table_name" => tbl_detentions, "db_field_names" => array("rel_student_uid", "detention_date"));

 

Database code:
 
$query = "INSERT INTO ". $database_info['db_table_name'];
$query .= " (" . $database_info['db_field_names'][0] . ", " . $database_info['db_field_names'][1] . ") VALUES ";
$query .= "($" . "rel_student_uid, '$detention_date')";
$result = mysql_query($query);
 
 
 
$query = "INSERT INTO ". $database_info['db_table_name'];
$query .= " (" . $database_info['db_field_names'][0] . ", " . $database_info['db_field_names'][1] . ") VALUES ";
$query .= "($rel_student_uid, '$detention_date')";
$result = mysql_query($query);
Link to comment
Share on other sites

$ in php marks the calling of a var weather it be array or what not if the $ is used improperly it will error out

 

$query .= "($" . "rel_student_uid, '$detention_date')";

 

reads the $ as a var call with no var name

 

$query .= "($rel_student_uid, '$detention_date')";

reads the $rel_student_uid as a var for php to call

 

any $ inside " " will be treated as a php var to avoid this if you are thing $ as just a character use mysql escape or enclose it in ' ' 

this making any $ inside a ' ' reas as text unless code escaped with ' ' . $var . ' ' or for doubles " " . $var . " " i personally always code escape my var and use mysql escape

 

i hope this helps

Link to comment
Share on other sites

Thanks it helps a bit though still haven't quite grasped. Tried making the original array variable have the $ instead of after the fact and using the mysql escape but still get the variable name $rel_student_uid instead of the actual uid.

 

 INSERT INTO tbl_detentions (rel_student_uid, detention_date) VALUES ('$rel_student_uid', '2013-3-3')

 

 

 

 

 

$database_info = array("db_table_name" => tbl_detentions, "db_field_names" => array("rel_student_uid", "detention_date"), "form_fields" => array('$rel_student_uid', "$detention_date"));
 

 

// Add detention to database BEGIN
$testt = mysql_real_escape_string($database_info['form_fields'][0]);

 

 

$query = "INSERT INTO ". $database_info['db_table_name'];
$query .= " (" . $database_info['db_field_names'][0] . ", " . $database_info['db_field_names'][1] . ") VALUES ";
$query .= "('" . mysql_real_escape_string($testt) . "', '$detention_date')";
$message .= $query;
$result = mysql_query($query);
if($result) {
$message .= "<center>Student given detention.</center>";
 
} else {
$message .= "Problem adding detention to student record.";
$message .= "<br>" . $query;
 
}
$detention_count++;
}
// Add detention to database END

 

Link to comment
Share on other sites

Yea, I'm starting to see that treating ' and " the same has finally caught up and bit me.  I tended to do it to avoid escape characters so I could have quotes inside quotes without the confusion of \ all over the place.  Still haven't figured out the right combination yet.  I can break things worse but can't seem to go in the other direction.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.