Jump to content

Recommended Posts

hi guys, as i am new to php

so i was making a simple script which will take the data from text field and send it to the database but the data is not inserting in table.i don't know why plz check.

here is the coding of form

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ADD Records</title>
</head>
<body>
<form action="addresults.php" method="post">
<table align="center" border="1">
<?
if(isset($_POST['Submit']))
{
echo "<tr align=\"center\"><td colspan=2>Records Added, ADD Another</td></tr>";
}
?>
<tr align="center">
<td colspan="2"><strong>::ENTER DATA IN RESPECTIVE FIELDS::</strong></td>
</tr>
<tr align="center">
<td width="104"><strong>Roll Number</strong></td>
<td width="316"><input type="text" name="rollno" /></td>
</tr>
<tr align="center">
<td><strong>Name</strong></td>
<td><input type="text" name="uname" /></td>
</tr>
<tr align="center">
<td><strong>Maths</strong></td>
<td><input type="text" name="maths" /></td>
</tr>
<tr align="center">
<td><strong>Physics</strong></td>
<td><input type="text" name="physics" /></td>
</tr>
<tr align="center">
<td><strong>Chemistry</strong></td>
<td><input type="text" name="chemistry" /></td>
</tr>
<tr align="center">
<td><strong>English</strong></td>
<td><input type="text" name="english" /></td>
</tr>
<tr align="center">
<td><strong>Hindi</strong></td>
<td><input type="text" name="hindi" /></td>
</tr>
<tr align="center">
<td colspan="2"><input type="submit" name="Submit" /></td>
</tr>
</table>
</form>
</body>
</html>

 

and here is the script which recieves the data (addresults.php)

<?php
$connect = mysql_connect("localhost","root","******")
or die(mysql_error());
$db = mysql_select_db("test",$connect)
or die(mysql_error());
$sql = "INSERT INTO results(rollno,name,maths,physics,chemistry,enlish,hindi) VALUES($_POST[rollno],\"$_POST[uname]\",$_POST[maths],$_POST[physics],$_POST[chemistry],$_POST[english],$_POST[hindi]";
$execquery = mysql_query($sql)
or die(mysql_error());
echo "Records Added";
?>

Link to comment
https://forums.phpfreaks.com/topic/200548-phpmysql-error-plz-help-newbie-here/
Share on other sites

oh thanks that was a quick reply however i did what you said but again the error appeared which says,

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\xampp\htdocs\project\addresults.php  on line 6

plz help....... :-[

When you put php array variables, i.e. $_POST['...'], inside of a double-quoted string, they must be enclosed in {} so that the php parser can find where they start and end. That will fix the php error message you are currently getting.

 

Any string data that is put into an sql query statement must be enclosed in single-quotes so that sql will treat it as a string.

 

If you are still getting a sql error, please post it as that will result in the quickest solution without a lot of guessing.

hi PFMaBiSmAd

thanks for your reply, i added the curly braces just as you said now my query looks like,

$sql = "INSERT INTO results(rollno,name,maths,physics,chemistry,enlish,hindi) VALUES({$_POST['rollno']},\"{$_POST['uname']}\",{$_POST['maths']},{$_POST['physics']},{$_POST['chemistry']},{$_POST['english']},{$_POST['hindi']}";

 

the error i got after this is,

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

Try:

$sql = 'INSERT INTO results(rollno,name,maths,physics,chemistry,enlish,hindi) VALUES('.$_POST['rollno'].', \''.$_POST['uname'].'\', '.$_POST['maths'].', '.$_POST['physics'].', '.$_POST['chemistry'].', '.$_POST['english'].', '.$_POST['hindi'].')';

 

this is assuming that name is the only string field in your database.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.