0

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.

4
Contributors
6
Replies
7
Views
6 Years
Discussion Span
Last Post by woofy613
0

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.

Edited by Caeon: messed up with that comment in there

0

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

0

Try this:

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

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.

0

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'.

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.