Forum Moderators: coopster

Message Too Old, No Replies

PHP and SQL

         

yoohoo

5:11 pm on Nov 7, 2003 (gmt 0)

10+ Year Member



HI there! I keep on getting this error message on the code below! What is wrong?

Error message:
Parse error: parse error, unexpected T_VARIABLE in C:\Program Files\Apache Group\Apache2\htdocs\form.php on line 22

And the code is:
<?
//Our PHP/MYSQL page.
//This script takes the data from the form
//fields and adds them to specified parts
//parts of the database
//MySQL Variables. Edit where necessary
$host = "localhost";
$login_name = "user";
$password = "pass";

//Connecting to MYSQL
MySQL_connect("$host","$login_name","$password");

//Select the database we want to use
MySQL_select_db("first_database") or die("Could not select database");

//Assign contents of form to variables
$name = $_POST['Name'];
$email = $_POST['Email'];

$sql = "INSERT INTO addressbook SET
Email = "$email",
First_Name = "$name"
");

$result = mysql_query($sql);

//Code to check if statement executed properly and display message
if ($result) {
echo("Email and Name successully added");
} else {
echo("An error has occured");
}

//Close connection with MySQL
MySQL_close()
?>

The problem seems to be in the 'Email = "$email",' thing but I don't know why!

Thanks a lot :>

Timotheos

5:18 pm on Nov 7, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to Webmaster World!

Yes, put the sql command all on one line.

Robber

6:11 pm on Nov 7, 2003 (gmt 0)

10+ Year Member



Watch out for your use of quotes:

$sql = "INSERT INTO addressbook SET
Email = "$email",
First_Name = "$name"
");

In the example above the quotes around $email will actually try and close the string as they match the opening quotes. You would need to change it to:

$sql = "INSERT INTO addressbook SET
Email = '$email',
First_Name = '$name'
");

yoohoo

8:00 pm on Nov 7, 2003 (gmt 0)

10+ Year Member



thanks a lot. this solved the problem.
however when i use the form below in order to attempt to add an entry to the database vai HTTP, the data is not entered in the table!

Although the ID is increasing, the First name and Email are not entered in the table.

Here is the form.php:

<html>
<body>
<form action="script.php">
Name: <input type="text" name="Name"><br>
Email: <input type="text" name="Email">
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>

Here is the script.php:

<?
//Our PHP/MYSQL page.
//This script takes the data from the form
//fields and adds them to specified parts
//parts of the database
//MySQL Variables. Edit where necessary
$host = "localhost";
$login_name = "user";
$password = "password";

//Connecting to MYSQL
MySQL_connect("$host","$login_name","$password");

//Select the database we want to use
MySQL_select_db("first_database") or die("Could not select database");

//Assign contents of form to variables
$name = $_POST['Name'];
$email = $_POST['Email'];

$sql = "INSERT INTO addressbook (Email,First_Name) VALUES ('$email','$name')";

$result = mysql_query($sql);

//Code to check if statement executed properly and display message
if ($result) {
echo("Email and Name successully added");
} else {
echo("An error has occured");
}

//Close connection with MySQL
MySQL_close()
?>

Any answers?

Thanks

coopster

8:13 pm on Nov 7, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You need to use POST in your form if you are going to use the $_POST superglobals array:

<form [b]method="post"[/b] action="script.php">

yoohoo

12:16 am on Nov 8, 2003 (gmt 0)

10+ Year Member



thanks a lot it worked :)

I now tried to make the whole script a bit more difficult:

<?
//Our PHP/MYSQL page.
//This script takes the data from the form
//fields and adds them to specified parts
//parts of the database
//MySQL Variables. Edit where necessary
$host = "localhost";
$login_name = "user";
$password = "pass";

//Connecting to MYSQL
MySQL_connect("$host","$login_name","$password");

//Select the database we want to use
MySQL_select_db("shops") or die("Could not select database");

//Assign contents of form to variables
$company = $_POST['Company'];
$owner = $_POST['Owner'];
$email = $_POST['Email'];
$address = $_POST['Address'];
$registration = $_POST['Date'];

$sql = "INSERT INTO client (PurchaseDate,Address,Email,Owner,Company) VALUES ('$registration','$address','$email,'$owner','$company')";

$result = mysql_query($sql);

//Code to check if statement executed properly and display message
if ($result) {
echo("You have been registered succesfully to our database! Thank you!");
} else {
echo("An error has occured");
}

//Close connection with MySQL
MySQL_close()
?>

BUT I GET AN 'ERROR HAS OCCURED' MESSAGE AT THE END. Why?

Shops is the database,
Client is the table and
Company,Owner,Email,Address,PurchaseDate are the five rows of the table.

In the web form, the labels for the above rows are: Company,Owner,Email,Address,Date respectively.

Thanks a lot :)

coopster

12:01 am on Nov 10, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



If your table name is Client with a capital 'C', you need to change your query to specify the table name the exact same way as MySQL tables are case sensitive:

INSERT INTO Client ...