Forum Moderators: coopster

Message Too Old, No Replies

Having trouble with query

syntex error?

         

Sarah Atkinson

5:40 pm on May 10, 2005 (gmt 0)

10+ Year Member



i can not figure out why this query keeps returning an error

$sql="INSERT INTO $fromwhere SET day='$date', maincourse='$maincourse', veggie='$veggie', veggie2='$veggietwo', fruit='$fruit', dessert='$dessert', drink='$drink', bread='$bread'";

kazecoder

5:51 pm on May 10, 2005 (gmt 0)

10+ Year Member



You have to specify WHERE:

$sql="INSERT INTO $fromwhere SET day='$date', maincourse='$maincourse', veggie='$veggie', veggie2='$veggietwo', fruit='$fruit', dessert='$dessert', drink='$drink', bread='$bread' WHERE id = 1";


WHERE id = 1 is just an example you can use any column and any value.

timster

5:55 pm on May 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You might be mixing up syntax for insert and update. An insert statement should look more like this:

$sql="INSERT INTO $fromwhere (day, maincourse )
VALUES ('$date', '$veggie')";

(I'm assuming you're using MySQL here -- is that correct?)

Sarah Atkinson

5:55 pm on May 10, 2005 (gmt 0)

10+ Year Member



fixed

kazecoder

5:56 pm on May 10, 2005 (gmt 0)

10+ Year Member



Sarah what did you change?

Sarah Atkinson

6:10 pm on May 10, 2005 (gmt 0)

10+ Year Member



ok i was wrong it doesn't work... I sware it worked once though

kazecoder

6:18 pm on May 10, 2005 (gmt 0)

10+ Year Member



Are you using mysql? Did you add the WHERE?

Sarah Atkinson

6:23 pm on May 10, 2005 (gmt 0)

10+ Year Member



I am using mysql. I don't know exsactly what I did the first time. I just re wrote it.

then after that didn't work(I first though it worked cause I didn't get an error message)

I have now re written it another way. I don't know what kind of where statement I would add cause this is a new entry

now the code is like :
if (isset($_POST['change'])):
$fromwhere=$_POST['fromwhere'];
$date=$_POST['date'];
$maincourse=$_POST['maincourse'];
$veggie=$_POST['veggie'];
$veggietwo=$_POST['veggie2'];
$fruit=$_POST['fruit'];
$dessert=$_POST['dessert'];
$drink=$_POST['drink'];
$bread=$_POST['bread'];




$sql = "INSERT INTO '$fromwhere'
(day, maincourse, veggie, veggie2, fruit, dessert, drink, bread)
VALUES ('$date','$maincourse','$veggie','$veggietwo','$fruit','$dessert','$drink','$bread')";

if (@mysql_query($sql)) {
echo '<p>Menu added</p>';
} else {
echo '<p>Error adding menu: ' .
mysql_error() . '</p>';
}

kazecoder

6:47 pm on May 10, 2005 (gmt 0)

10+ Year Member



Ok I am sorry I misread your initial post I didnt realize you were inserting a new row...sorry.

$sql = "INSERT INTO $fromwhere VALUES ('$date','$maincourse','$veggie','$veggietwo','$fruit','$dessert','$drink','$bread')";

$result=mysql_query($sql);

As long as your values are in the same order as your
table is setup you dont need the other part of the query that you had with the parentheses. Try the above.

Sarah Atkinson

2:57 pm on May 11, 2005 (gmt 0)

10+ Year Member



what aboult id?
it is an auto incriment will it just make itself?

Sarah Atkinson

3:02 pm on May 11, 2005 (gmt 0)

10+ Year Member



still doesn't work. here is the error message i get

Error adding menu: 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 'VALUES ('2005-05-10','Rost Pork','','','','','','')' at line 1

kazecoder

3:07 pm on May 11, 2005 (gmt 0)

10+ Year Member



It will auto increment by itself but you still have to include it the column in the query. I am assuming its the first column in the table? You have to include the single quotes with no value or variable like this:

$sql = "INSERT INTO $fromwhere VALUES ('','$date','$maincourse','$veggie','$veggietwo','$fruit','$dessert','$drink','$bread')";

$result=mysql_query($sql);

Sarah Atkinson

3:19 pm on May 11, 2005 (gmt 0)

10+ Year Member



IT still does not work....

here is the whole code. I'm wandering if it is just a missing quote or ; somewhere?


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Meals for Better Living Menu Admin</title>
</head>

<body>

<?php //connects to the server and database

include_once 'db.inc.php';

if (isset($_POST['change'])):
$fromwhere=$_POST['fromwhere'];
$date=DATE($_POST['date']);
$maincourse=$_POST['maincourse'];
$veggie=$_POST['veggie'];
$veggietwo=$_POST['veggie2'];
$fruit=$_POST['fruit'];
$dessert=$_POST['dessert'];
$drink=$_POST['drink'];
$bread=$_POST['bread'];

$sql = "INSERT INTO $fromwhere VALUES ('','$date','$maincourse','$veggie','$veggietwo','$fruit','$dessert','$drink','$bread')";

$result=mysql_query($sql);

if ($result) {
echo '<p>Menu added</p>';
} else {
echo '<p>Error adding menu: ' .
mysql_error() . '</p>';
}

?>
<p><a href="<?php echo $_SERVER['PHP_SELF'];?>?fromwhere=meals_menus_bbjj"> Add another menu for Bartholomew, Brown, Jackson, and Jennings Counties. </a></p><br />
<p><a href="<?php echo $_SERVER['PHP_SELF'];?>?fromwhere=meals_menus_d"> Add another menu for Decatur County. </a></p>

<?php elseif (isset($_GET['fromwhere'])):
$fromwhere=$_GET['fromwhere'];
if($fromwhere==meal_menus_bbjj)
echo '<p> Enter menu for Bartholomew, Brown, Jackson, and Jennings Counties.</p>';
elseif($fromwhere==meal_menus_d)
echo '<p> Enter menu for Decatur County.</p>';
else
echo '<p>menu error.</p>';?>

<form action="addmenu.php" method="post">
<label>Date (y-m-d): <input type="text" name="date" /></label><br />
<label> Main Course: <input type="text" name="maincourse" /></label><br />
<label> Vegetable #1: <input type="text" name="veggie" /></label><br />
<label> Vegetable #2: <input type="text" name="veggie2" /></label><br />
<label> Fruit: <input type="text" name="fruit" /></label><br />
<label> Bread: <input type="text" name="bread" /></label><br />
<label> Dessert: <input type="text" name="dessert" /></label><br />
<label> Drink: <input type="text" name="drink" /></label><br />
<input type="hidden" name="fromwhere" value="<?php $fromwhere?>" />
<input type="hidden" name="change" value="1" />
<input type ="submit" value="SUBMIT"/>
</form>

<?php else: //if no area has been selected?>
Select area menu you would like to update:
<p><a href="addmenu.php?fromwhere=meal_menus_bbjj">Add a menu for Bartholomew, Brown, Jackson, and Jennings Counties. </a></p>
<p><a href="addmenu.php?fromwhere=meal_menus_d">Add a menu for Decatur County.</a></p>

<?phpendif;?>

</body>
</html>

Sarah Atkinson

4:07 pm on May 11, 2005 (gmt 0)

10+ Year Member



figured it out

<input type="hidden" name="fromwhere" value="<?php $fromwhere?>" />
had to be
<input type="hidden" name="fromwhere" value="<?php echo "$fromwhere";?>" />

Duhh