Jump to content

Not sure why this insert is failing using "$result = $conn->query()"


Recommended Posts

An insert function is not inserting.  I keep getting "Could not store data, please try again."

 

After some probing, it seems that "$result = $conn->query($user_degree_insert_query); " is where the problem is.  Variables before it seem to pass testing.  I can't figure out why this line is messing thins up though.

 

Any idea?  Thank you in advance for your help!

 

The "//$degree_id = mysql_fetch_array($degree_id_query, MYSQL_BOTH) ;" is commented out because it was giving me trouble, i replaced it with the line above that

 

                $conn = db_connect();

	$arraycount = count($degree_Array);

	//loop through the categories chosen and add them each into member-category
	for($i =0; $i < $arraycount; $i++){ 
		$degree_id_query = "SELECT degree_id FROM degree WHERE degree_type ='".$degree_Array[$i]."'";
		$result = $conn->query($degree_id_query);

		//place the id found into the array, which automatically cancatonates it.
		$degree_id = $result->fetch_array();

		//$degree_id = mysql_fetch_array($degree_id_query, MYSQL_BOTH) ;

		$user_degree_insert_query = "INSERT INTO user-degree (
									`user_id` ,
									`degree_id`
									)
								VALUES (
									'".$user_id."', '".$degree_id[0]."'
									)";	

		$result = $conn->query($user_degree_insert_query); 	

		if($result) {
			header("Location: survey2.php"); 
		}else { 
			echo "<p>Could not store data, please try again.</p>"; 
			exit;

Does this line in your code work?

 

 	

$degree_id_query = "SELECT degree_id FROM degree WHERE degree_type ='".$degree_Array[$i]."'";

 

Surely if you wrap a double quoted variable in single quotes it will be taken as a literal and not the value it represents?

 

 

 

 

Gosh, i just figured it out. 

 

$user_degree_insert_query = "INSERT INTO `user-degree`(

 

user-degree didn't have the surrounding  ``.   

 

Why is it that sometimes it seems i need these and sometimes not.

Database, table, and column names may only contain alphanumeric characters and the under_score character. Using any other characters in the name, having an all numeric name, or a name that is a reserved mysql keyword requires special handling. The use of back-ticks `` around the name identifies it as a database, table, or column name that requires special handling. We generally suggest using a different name that doesn't require special handling so that you don't need to use any extra syntax to make them work.

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.