Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #3' at line 1

Here is the code that I have can someone help me with this error, I have redone this a ton and not sure what the problem is.


PHP
<?php
require_once("database.php");

$sql = "INSERT INTO Recipe(Category, Recipe Name, Ingredients, Preparations, Cooking Time, Servings, Optional Notes) VALUES ('$category','$recipename','$ingredients','$preparations','$cookingtime','$servings','$optionalnotes')";
$sql = mysql_query("SELECT * FROM Recipe",$conn) or die("Problem reading table: " .mysql_error());

    if (!mysql_query($sql,$conn))
  {
        die('Error: ' . mysql_error());
  }
        echo "1 record added";
        mysql_close($con)


?>
Posted
Comments
Henry Minute 18-Mar-11 19:44pm    
You appear to have ignored the first answer that you received, which from my perspective highlighted the root cause of your problem.

Is there a reason for this?

First, you assign a string to a variable:
$sql = "INSERT INTO Recipe...";

That is perfectly fine, but then you overwrite that same variable with a new value without using that first value you assigned:
$sql = mysql_query("SELECT * FROM Recipe", $conn) or ...;

And then you use the result of that query (which was stored in $sql) and you execute it as a query:
if (!mysql_query($sql,$conn))

That makes little sense. I think perhaps you are a little confused.
 
Share this answer
 
$sql = mysql_query("SELECT * FROM Recipe",$conn) or die("Problem reading table: " .mysql_error());


remove that line. Also, your $conn variable is incorrectly spelled.
 
Share this answer
 
v2
Comments
Member 7666290 18-Mar-11 18:12pm    
ok I made the changes that you suggested and now i get this error, any suggestions?

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Name, Ingredients, Preparations, Cooking Time, Servings, Optional Notes) VALUES ' at line 1
walterhevedeich 20-Mar-11 17:47pm    
You may need to print the actual query(not the one with variables, but with the values) in order for us to see where it erred at. Some of the variables may have invalid values in it.
Examine the content of $sql in debug mode immediately before the code executes it. Put a watch on it and from the list of choices take text visualizer. This will show the actual sql being executed. Now paste this sql directly into something like MySQL query browser or DB Visualizer, where you should execute it.

Now that you are executing your sql away from the 'noise' of your code, its much easier to see where the faults are and correct them. Once you know whats wrong, you may correct your code.
 
Share this answer
 
Comments
Member 7666290 18-Mar-11 18:41pm    
ok i am a beginner here so what do you mean run it in debug mode before it executes how do I do that? Sorry I am just learning this stuff.
Ger Hayden 18-Mar-11 18:53pm    
Most development enviromnets have an option to run in debug mode e.g. F5 will do it in Visual Studio.

Now if you are working from the command line it may not be possible, then I recommend writing your query to the screen immediately before you execute it so that you can copy it from there.
Member 7666290 20-Mar-11 11:51am    
ok this is what I have and it will on post the category, recipename, servings. in the cooking time it will always put 0 no matter what number i put in there, and it won't post the ingredients, preparations and optional notes and I am not sure why. here is my code now:



Submitted



<p>Thank you, your recipe has been submitted.
back to main
</p>


<form action="

<table border="0">

<tr><td>Category:</td><td>

<input type="text" name="category" maxlength="30">

</td></tr>


<tr><td>Recipe Name:</td><td>

<input type="text" name="recipename" maxlength="30">

</td></tr>

<tr><td>Ingredients:</td><td>

<textarea name="Ingredients" cols="40" rows="5" maxlength="255"></textarea>

</td></tr>

<tr><td>Preparations:</td><td>

<textarea name="Preparations" cols="40" rows="5" maxlength="255"></textarea>

</td></tr>

<tr><td>Cooking Time:</td><td>

<input type="cookingTime" name="cookingTime" maxlength="10">

</td></tr>

<tr><td>Servings:</td><td>

<input type="Servings" name="servings" maxlength="10">

</td></tr>

<tr><td>Optional Notes:</td><td>

<textarea name="Optional Notes" cols="40" rows="5" maxlength="255"></textarea>

</td></tr>

<tr><th colspan=2><input type="submit" name="submit"
value="Submit"><input type="reset" name="clear" id="clear" value= "reset" /></th></tr> </table>
</form>





</body>
</html>
XML
<?php
    require_once("database.php");
 if (isset($_POST['submit']))
 {


    $insert = "INSERT INTO Recipe (category, recipename, ingredients, preparations, cookingtime, servings, optionalnotes)
    VALUES ('".$_POST['category']."', '".$_POST['recipename']."', '".$_POST['ingredients']."', '".$_POST['preparations']."',
    '".$_POST['cookingtime']."','".$_POST['servings']."','".$_POST['optionalnotes']."')";
    $add_recipe = mysql_query($insert);

 }
 ?>
  <h1>Submitted</h1>
 <p>Thank you, your recipe has been submitted.
 <a href="membersarea.html">back to main</a>
</p>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
 <table border="0">
 <tr><td>Category:</td><td>
 <input type="text" name="category" maxlength="30">
 </td></tr>

 <tr><td>Recipe Name:</td><td>
 <input type="text" name="recipename" maxlength="30">
 </td></tr>
 <tr><td>Ingredients:</td><td>
 <textarea name="Ingredients" cols="40" rows="5" maxlength="255"></textarea>
 </td></tr>
 <tr><td>Preparations:</td><td>
 <textarea name="Preparations" cols="40" rows="5" maxlength="255"></textarea>
 </td></tr>
 <tr><td>Cooking Time:</td><td>
 <input type="cookingTime" name="cookingTime" maxlength="10">
 </td></tr>
 <tr><td>Servings:</td><td>
 <input type="Servings" name="servings" maxlength="10">
 </td></tr>
 <tr><td>Optional Notes:</td><td>
<textarea name="Optional Notes" cols="40" rows="5" maxlength="255"></textarea>
 </td></tr>
 <tr><th colspan=2><input type="submit" name="submit"
 value="Submit"><input type="reset" name="clear" id="clear" value= "reset" /></th></tr> </table>
</form>


</body>
</html>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900