ityler22
ityler22

Reputation: 372

Error: Unknown column in 'where clause'

I have a form that is populated with mySQL db values upon loading the page. The data in the form can be edited and then a submit button that send the data to mysql to overwrite the values that were preloaded in the form. The fields are displayed in the form correctly but when changed and submitted I receive the following: Error:Unkown column in 'where clause'.

I'm relatively new to working with mysql query's instead of taking the 'phpmyadmin' route ._.

Here's the query I used to fill the table with the original form data:

INSERT INTO mytable VALUES ("sunday", "name1", "price1"), ("monday", "name2", "$35"), ("tuesday", "name3", "$100"), ("wednesday", "name4", "$65"), ("thursday", "name5", "$5"), ("friday", "name6", "$57"), ("saturday", "name7", "$10");

Here's the sql query used to populate the form and then assigned to a variable by usingfetch_array $sun_data = mysql_query("SELECT * FROM mytable WHERE day='sunday'") or die(mysql_error()); ... ...

$sun_info = mysql_fetch_array( $sun_data );

Then the form:

<form action="update_form.php"> <input type="text" name="sun_name" value="<?php echo $sun_info['name'] ?>" /> <input type="text" name="sun_price" value="<?php echo $sun_info['price'] ?>" />

Like I said the form updates fine but then when the form data is submitted to update_form.php I receive the error. Here is the form action update.php

$sun_day_change = $_POST['sun_name']; $sun_price_change = $_POST['sun_price']; $sunday = 'sunday';

$sql = "UPDATE mytable SET name = '$sun_day_change', price = '$sun_price_change' WHERE day = ".$sunday;

if (!mysql_query($sql)) { die('Error: ' . mysql_error()); }

header('Location: http://localhost/form.php');

I'm assuming the issue is when I pass the values back to the database but since I have used almost the identical sql query to retrieve the values I'm not sure where I went wrong. Thanks for the help!

Upvotes: 0

Views: 1646

Answers (1)

Andreas Hagen
Andreas Hagen

Reputation: 2325

You are missing quotation marks in the update query, around $sunday.

$sql = "UPDATE mytable SET name = '$sun_day_change', price = '$sun_price_change' WHERE day = '".$sunday."'";

Upvotes: 1

Related Questions