Jump to content

problem with 'insert into' statement


nawoc

Recommended Posts

Hi, I'm new to PHP+MySQL, have been following some online tutorials but am a bit stuck on this one.

Any help appreciated :)

 

Have previously made the database the code refers to and populated it through PHPMyAdmin. Have connected previously through PHP to use a SELECT statement successfully but am failing to use INSERT INTO correctly.

 

The error I get is:

Error in query: INSERT INTO symbols (country, animal) VALUES ('test', test').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 '')' at line 1

 

The Code:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<basefont face="Arial" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<?php

	if (!isset($_POST['submit'])) {
		// form not submitted
?>
    
    		<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
            Country: <input type="text" name="country" />
            National animal: <input type="text" name="animal" />
            <input type="submit" name="submit" />
            </form>
            
    <?php
	}
	else {
		// form submitted
		// set server access variables
		$host = "localhost";
		$user = "user";
		$pass = "pass";
		$db = "testdb";

		// get form input
		// check to make sure it's all there
		// escape input values for greater safety
		$country = empty($_POST['country']) ? die ("Error: Enter a country") :
			mysql_escape_string($_POST['country']);
		$animal = empty($_POST['animal']) ? die ("Error: Enter an animal") :
			mysql_escape_string($_POST['animal']);

		// open connection
		$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

		// select database
		mysql_select_db($db) or die ("Unable to select database!");

		// create query
		$query = "INSERT INTO symbols (country, animal) VALUES ('$country', $animal')";

		// execute query
		$result = mysql_query($query) or die("Error in query: $query.".mysql_error());

		// print message with ID of inserted record
		echo "New record inserted with ID ".mysql_insert_id();

		// close connection
		mysql_close($connection);
	}
?>


</body>
</html>

 

 

Thanks for any help :)

Link to comment
https://forums.phpfreaks.com/topic/191362-problem-with-insert-into-statement/
Share on other sites

You are missing a quote, as your error message is telliing you

 

$query = "INSERT INTO symbols (country, animal) VALUES ('$country', $animal')";

       

 

should be

 

$query = "INSERT INTO symbols (country, animal) VALUES ('$country', '$animal')";

       

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.