I am getting the following error: Parse error: syntax error, unexpected T_STRING

The code snippet:

$db = "hospital";
$cxn = mysqli_connect($host,$user,$password,$db);
$sql1 =  INSERT INTO intake2(site, Record, BMI, Weight) VALUES($_POST[\"site\"], $_POST[\"Record\"], $_POST[\"BMI\"], $_POST[\"Weight\"]); [TEX]// this is the line that generates the error[/TEX]

$qyr = mysqli_query($cxn,$sql1);

I realize that the error is probably improper escaping of the query. I've tried everything and am still getting the same error.

Thanks in advance.

You forgot quotes around the sql and semicolon on line 3.

$db = "hospital";
$cxn = mysqli_connect($host,$user,$password,$db);
$sql1 =  "INSERT INTO intake2(site, Record, BMI, Weight) VALUES($_POST[\"site\"], $_POST[\"Record\"], $_POST[\"BMI\"], $_POST[\"Weight\"])";
$qyr = mysqli_query($cxn,$sql1);

Hope that helps. Have a good one.

You forgot quotes around the sql and semicolon on line 3.

$db = "hospital";
$cxn = mysqli_connect($host,$user,$password,$db);
$sql1 =  "INSERT INTO intake2(site, Record, BMI, Weight) VALUES($_POST[\"site\"], $_POST[\"Record\"], $_POST[\"BMI\"], $_POST[\"Weight\"])";
$qyr = mysqli_query($cxn,$sql1);

Hope that helps. Have a good one.

Thanks.

I used your edit. I now get the message Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /var/www/test_db2.php on line 21

Now what?

Thanks in advance

Try this:

$sql1 =  "INSERT INTO intake2(site, Record, BMI, Weight) VALUES('$_POST[site]', '$_POST[Record]','$_POST[BMI]','$_POST[Weight]')";

Try this:

$sql1 =  "INSERT INTO intake2(site, Record, BMI, Weight) VALUES('$_POST[site]', '$_POST[Record]','$_POST[BMI]','$_POST[Weight]')";

I tried the suggested code. The error is now:
Notice: Undefined index: site in /var/www/test_db2.php on line 21

Notice: Undefined index: Record in /var/www/test_db2.php on line 21

Notice: Undefined index: BMI in /var/www/test_db2.php on line 21

Notice: Undefined index: Weight in /var/www/test_db2.php on line 21
$_POST

I do not know if this will help, but here is the code that generated the form:

<html>
<head>
<title>Test of Form3</title>
<center><h4></b>Intake Data</h3></center>
</head>


<body>
<form>

<form action="test_db2.php" method="post" />
Site: <input type="text" name="site" />
Record Number: <input type="decimal" name="Record" />
BMI: <input type="decimal" name="BMI" />
Weight: <input type="decimal" name="Weight" />
<input type="submit" />
</form>
</body>
</html>

Thanks again.

Use 'isset()' method to check the '$_POST' vars are already defined.

if(isset($_POST['site']) && isset($_POST['Record']) && isset($_POST['BMI']) && isset($_POST['Weight'])){
$site = $_POST['site'];
$record = $_POST['Record'];
$bmi = $_POST['BMI'];
$weight = $_POST['weight'];
$sql1 =  "INSERT INTO intake2(site, Record, BMI, Weight) VALUES('$site', '$record','$bmi','$weight')";

$qyr = mysqli_query($cxn,$sql1);
}

You can use '$_REQUEST' for both '$_GET' and '$_POST'.

Use 'isset()' method to check the '$_POST' vars are already defined.

if(isset($_POST['site']) && isset($_POST['Record']) && isset($_POST['BMI']) && isset($_POST['Weight'])){
$site = $_POST['site'];
$record = $_POST['Record'];
$bmi = $_POST['BMI'];
$weight = $_POST['weight'];
$sql1 =  "INSERT INTO intake2(site, Record, BMI, Weight) VALUES('$site', '$record','$bmi','$weight')";

$qyr = mysqli_query($cxn,$sql1);
}

You can use '$_REQUEST' for both '$_GET' and '$_POST'.

Thanks.

It appears that the variables are not set since I get an "undefined index" error if I use the command $site = $_POST;. Using the if(isset..it would be non-productive since I need to have the varibles to add to the database.

The question is how to set the variables in the form.

Again, thanks in advanace.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.