Forum Moderators: coopster
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 :>
$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'
");
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
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 :)