Note that there are some explanatory texts on larger screens.

plurals
  1. POA PHP MySQL Error which I just cannot work out
    primarykey
    data
    text
    <p>I'm playing with PHP/MySQL and trying to teach myself online, but can't figure this out for the life of me.</p> <p>I have been following a PHP tutorial at <a href="http://www.keithjbrown.co.uk/vworks/php/php_p5.php" rel="nofollow"><a href="http://www.keithjbrown.co.uk/vworks/php/php_p5.php" rel="nofollow">http://www.keithjbrown.co.uk/vworks/php/php_p5.php</a></a></p> <p>This page is at tasmanianracing.com/horses.php</p> <p>I'm getting the following mysql error:</p> <blockquote> <p>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 'WHERE (horses.horseID = )' at line 1</p> </blockquote> <p>which is being thrown up from my function update_horse()</p> <p>My code is below - if someone could help me out I'd be forever grateful!</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;Horses | Horse Database&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;?php if (!$_REQUEST['Submit']) { html_form(); } elseif ($_REQUEST['Submit'] == "View Horse") { select_horse(); } elseif ($_REQUEST['Submit'] == "Edit") { get_data(); } elseif ($_REQUEST['Submit'] == "Update") { update_horse(); } function my_conn() { /* sets the variables for MySQL connection */ $server = "***"; // this is the server address and port $username = "***"; // this is the mysql username $password = "***"; // this is the mysql password /* connects to the MySQL server */ $link = @mysql_connect ($server, $username, $password) or die (mysql_error()); /* defines the active database for the connection */ if (!@mysql_select_db("tashorse_tasform", $link)) { echo "&lt;p&gt;There has been an error. This is the error message:&lt;/p&gt;"; echo "&lt;p&gt;&lt;strong&gt;" . mysql_error() . "&lt;/strong&gt;&lt;/p&gt;"; echo "Please contact your systems administrator with the details"; } return $link; } function html_form() { ?&gt; &lt;p&gt;Please enter the search term for the horse&lt;/p&gt; &lt;form name="horsesearch" method="post" action="&lt;? echo $_SERVER['PHP_SELF']; ?&gt;"&gt; Name of horse: &lt;input type="text" name="horse_name"&gt; &lt;input type="submit" name="Submit" value="View Horse" /&gt; &lt;/form&gt; &lt;? } function select_horse() { ?&gt; &lt;h4&gt;Horse Search&lt;/h4&gt; &lt;? $conn = my_conn(); /* Sets the SQL Query */ $sql = "SELECT * FROM horses"; $sql .= " WHERE (horses.horse_name = '{$_POST['horse_name']}')"; /* Passes a Query to the Active Database */ $result = mysql_query($sql, $conn); if (!$result) { echo("&lt;p&gt;Error performing query: " . mysql_error() . "&lt;/p&gt;"); exit(); } /* starts the table and creates headings */ ?&gt; &lt;table&gt; &lt;tr&gt; &lt;td&gt;&lt;strong&gt;Horse Name&lt;/strong&gt;&lt;/td&gt; &lt;td&gt;&lt;strong&gt;Year Foaled&lt;/strong&gt;&lt;/td&gt; &lt;td&gt;&lt;strong&gt;Trainer&lt;/strong&gt;&lt;/td&gt; &lt;td&gt;&lt;strong&gt;Owners&lt;/strong&gt;&lt;/td&gt; &lt;td&gt;&lt;strong&gt;Silks&lt;/strong&gt;&lt;/td&gt; &lt;td&gt;&lt;/td&gt; &lt;/tr&gt; &lt;? /* retrieves the rows from the query result set and puts them into a HTML table row */ while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo("&lt;tr&gt;&lt;td&gt;" . $row["horse_name"] . "&lt;/td&gt;"); echo("&lt;td&gt;" . $row["year_foaled"] . "&lt;/td&gt;"); echo("&lt;td&gt;" . $row["trainer"] . "&lt;/td&gt;"); echo("&lt;td&gt;" . $row["owners"] . "&lt;/td&gt;"); echo("&lt;td&gt;" . $row["silks"] . "&lt;/td&gt;"); echo("&lt;td&gt;&lt;a href=\"" . $_SERVER['PHP_SELF'] . "?horseID=" .$row['horseID'] . "&amp;Submit=Edit\"&gt;Edit&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;"); } /* closes the table */ ?&gt; &lt;/table&gt; &lt;? /* closes connection to the MySQL server */ mysql_close ($conn); /* Displays HTML Form */ html_form(); } function get_data() { /* Calls our connection function */ $conn = my_conn(); /* Defines query */ $sql = "SELECT * FROM horses WHERE (horses.horseID = " . $_REQUEST['horseID'] . ")"; /* Passes query to database */ $result = mysql_query($sql, $conn); if (!$result) { echo("&lt;p&gt;Error performing query: " . mysql_error() . "&lt;/p&gt;"); exit(); } /* creates our row array with an if statement to report errors */ if ($row = @mysql_fetch_array($result, MYSQL_ASSOC)) { /* prints out the horse name */ print "&lt;h4&gt;$row[horse_name]&lt;/h4&gt;"; /* prints out our HTML form '\"' */ print "&lt;form name=\"horseupdate\" method=\"post\" action=\"$_SERVER[PHP_SELF]\"&gt;"; /* prints out our HTML table and fields 'escaping' any double quotes '\"' */ print "&lt;table width=\"600\"&gt; &lt;tr&gt; &lt;td width=\"150\"&gt;&lt;strong&gt;Horse Name&lt;/strong&gt;&lt;/td&gt; &lt;td width=\"350\"&gt;&lt;input type=\"hidden\" name=\"horse_name\" value=\"$row[horse_name]\"&gt;&lt;/td&gt; &lt;td rowspan=\"5\" valign=\"top\"&gt; &lt;input type=\"submit\" name=\"Submit\" value=\"Update\"&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td width=\"150\"&gt;&lt;strong&gt;Year Foaled&lt;/strong&gt;&lt;/td&gt; &lt;td width=\"350\"&gt;&lt;input type=\"text\" size =\"4\" name=\"year_foaled\" value=\"$row[year_foaled]\"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td width=\"150\"&gt;&lt;strong&gt;Trainer&lt;/strong&gt;&lt;/td&gt; &lt;td width=\"350\"&gt;&lt;input type=\"text\" size =\"40\" name=\"trainer\" value=\"$row[trainer]\"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td width=\"150\"&gt;&lt;strong&gt;Owners&lt;/strong&gt;&lt;/td&gt; &lt;td width=\"350\"&gt;&lt;input type=\"text\" size =\"40\" name=\"owners\" value=\"$row[owners]\"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td width=\"150\"&gt;&lt;strong&gt;Silks&lt;/strong&gt;&lt;/td&gt; &lt;td width=\"350\"&gt;&lt;input type=\"text\" size =\"40\" name=\"silks\" value=\"$row[silks]\"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/form&gt;"; } else { echo("There has been an error" . mysql_error()); } /* closes connection */ mysql_close ($conn); } function update_horse() { /* Calls our connection function */ $conn = my_conn(); /* Defines query */ $sql_update = "UPDATE horses SET "; $sql_update .= "horses.year_foaled = '" . $_REQUEST['year_foaled'] . "', "; $sql_update .= "horses.trainer = '" . $_REQUEST['trainer'] . "', "; $sql_update .= "horses.owners = '" . $_REQUEST['owners'] . "', "; $sql_update .= "horses.silks = '" . $_REQUEST['silks'] . "', "; $sql_update .= "WHERE (horses.horseID = " . $_REQUEST['horseID'] . ")"; /* Passes query to database */ $result = mysql_query($sql_update, $conn); if (!$result) { echo("&lt;p&gt;Error performing query: " . mysql_error() . "&lt;/p&gt;"); exit(); } /* Prints success message */ print "&lt;p&gt; Successfully Updated&lt;/p&gt;"; /* closes connection */ mysql_close ($conn); /* Calls get_data() function */ getdata(); } ?&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload