I'm new to PHP and get the message Parse error: syntax error, unexpected T_VARIABLE in .. on line 20. I would be ever so grateful if someone could help me with this error as it's really started to stress me out. Been on dreamweaver and it says error where it says $Username=... But I just can't seem to fix it
<?php
$host="localhost"; // Host name
$username="xxx"; // Mysql username
$password="xxx"; // Mysql password
$db_name="xxx"; // Database name
$tbl_name="avaya"; // Table name
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("inventory", $con)
$addavaya="INSERT INTO avaya_pabx(critical_spare_id, serial_no, ,comcode, version, circuit_pack, classification, location, availability, date, client)
VALUES ('$_POST[critical_spare_id]', '$_POST[serial_no]', '$_POST[comcode]', '$_POST[version]', '$_POST[circuit_pack]',
'$_POST[classification]', '$_POST[location]' , '$_POST[availability]', '$_POST[date]', '$_POST[client]')";
mysql_query($addavaya,$con)
if (!mysql_query($addavaya,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con);
?>
You have a missing semi colon:
mysql_select_db("inventory", $con);
^
You should concatenate the query:
$addavaya="INSERT INTO avaya_pabx(critical_spare_id, serial_no, ,comcode, version, circuit_pack, classification, location, availability, date, client)
VALUES ('". $_POST['critical_spare_id'] . "', '" . $_POST['serial_no']. "', etc...
Also please stop using the old mysql_*
functions. Use either mysqli_*
or PDO. mysql_* functions will be deprecated in the future.
And please please please sanitize the input before querying the database!
$_POST
is an associative array, so you need to be accessing it using single quotes, for example:
$_POST['critical_spare_id'] instead of $_POST[critical_spare_id]
This means you will have to use the concatenation operator in your query.
$addavaya="INSERT INTO avaya_pabx(critical_spare_id, serial_no, ,comcode, version, circuit_pack, classification, location, availability, date, client)
VALUES ('" . $_POST['critical_spare_id'] . "', ...
Alternatively (and for your own good), you could use parameterized queries with PDO or mysqli.
Add a ;
to the end of mysql_select_db("inventory", $con)