0

编辑:我可以看到表单,但它没有写入数据库。这是代码:

<html>
<head>Test PAge</head>
<body>
<form action="testform3.php" method="POST">
<h1 align="center">Client Registration</h1><br>
<br>
<table>
<tr><td>
CID: <input type = "text" name="cid">
</td></tr>
<tr><td>Client Name: <input type="text" name="cname"></td></tr>
<tr><td>Client Short Name: <input type="text" name="cshortname"></td></tr>
<tr><td>Representative/Coordinator: <input type="text" name="coordinator"></td></tr>
<tr><td>BusinessHead: <input type=text name="bizhead"></td></tr>
<tr><td>Mobile: <input type=text name="mob_no"></td></tr>
<tr><td>[Phone (1)]: <input type=text name="phone1"></td>
<td>[Phone (2)]: <input type=text name="phone2"></td>
<td>[Fax (1)]: <input type=text name="fax1"></td>
<td>[Fax (2)] : <input type=text name="fax2"></td>
</tr>
<tr><td>Area : <input type=text name="area"></td>
<td>City : <input type=text name="city"></td>
<td>[Postal Code] : <input type=text name="postalcode"></td>
<td>[Zip Code] : <input type=text name="zipcode"></td></tr>
<tr><td>State : <input type=text name="state"></td>
<td>Country : <input type=text name="country"></td></tr>

<tr><td>
Remarks : <input type=text name="remarks">
</td></tr>
<tr><td><input type="submit" name="savetoDB" value="registerclient"></td></tr>
</table>
</form>
</body>
</html> 

<?php
if (isset($_POST['savetoDB']))
{

$host="localhost:3306"; // Host name with mysql port no
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="mediaplus_test"; // Database name
$tbl_name="clientele"; // Table name

// Connect to server and select databse.


$con = mysql_connect("$host", "$username", "$password");
if ( ! $con )
{
die("Cannot connect to database....!" . mysql_error());
}
echo "connected";


 //Selecting the database to insert form data to


mysql_select_db("$db_name",$con) or die("cannot select DB...!"  . mysql_error());
echo "connected to database";

//SQL Querty to write to database. using a variable to store sql insert query

extract($_POST); 

$sql = "INSERT INTO    (ID, ClientFullName, ClientShortName, Representative, 
BusinessHead, Mobile, Phone (1), Phone (2), Fax(1), Fax (2), Area, City, State, 
Country, Postal Code, Zip Code, Remarks) " .
 "VALUES  

('$cid', '$cname', '$cshortname', '$coordinator', '$bizhead', '$mob_no', '$phone1',   
 '$phone2', '$fax1', '$fax2', '$area', '$city', '$state', '$country', '$postalcode', 
'$zipcode', '$remarks')";


    //'coordinator']."','".$_POST['bizhead']."','".$_POST['mob_no']."','".$_POST['phone1']."','//".$_POST['phone2']."','".$_POST['fax1']."','".$_POST['fax2']."','".$_POST['area']."','".$//_POST['city']."','".$_POST['postalcode']."','".$_POST['zipcode']."','".$_POST['state']."'//,'".$_POST['country']."','".$_POST['remarks']."')";

mysql_query($sql,$con);
mysql_close($con);
}
//Echo "Data inserted into clientele";
?>

问题到此结束..!


我正在尝试使用 php 将表单输入中的数据插入 mysql 数据库。我在浏览器中收到以下错误:

Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in J:\xampp\htdocs\testform.php on line 70

这是我的查询:

sql = "INSERT INTO clientele(ID,ClientFullName,ClientShortName,Representative,BusinessHead,Mobile,Phone (1),Phone (2),Fax(1),Fax (2),Area,City,State,Country,Postal Code,Zip Code,Remarks)" .
"VALUES ('$_POST'[cid]',
'$_POST['cname']', '$_POST['cshortname']',
'$_POST['coordinator']',
'$_POST['bizhead']',
'$_POST['mob_no']',
'$_POST['phone1']',
'$_POST['phone2']',
'$_POST['fax1']',
'$_POST['fax2']',
'$_POST['area']',
'$_POST['city']',
'$_POST['postalcode']',
'$_POST['zipcode']',
'$_POST['state']',
'$_POST['country']',
'$_POST['remarks']',)";

这里可能是什么问题?谢谢。

4

4 回答 4

1

我不喜欢重写你的洞查询。

这是一个示例,如何在没有语法错误的情况下完成:

$sql = "INSERT INTO clientele(`ID`,`ClientFullName`, `ClientShortName`) " .
"VALUES ('".$_POST['cid']."','".$_POST['cname']."','".$_POST['cshortname']."')";

但是你必须关心 SQL 注入问题!!!不要这样放到网上!

于 2013-08-26T15:37:20.427 回答
0

尝试输入值

'{$_POST['cid']}'
于 2013-08-26T15:36:44.307 回答
0
'$_POST'[cid]'

不了解你,但这看起来对我来说不会很好。

也修改:

'$_POST['cid']'
于 2013-08-26T15:34:58.080 回答
0

为避免使查询复杂化,请使用 extract(),因此@steven 的答案将很简单,如下所示:

extract($_POST); 

$sql = "INSERT INTO clientele(`ID`,`ClientFullName`, `ClientShortName`) VALUES ('$cid','$cname','$cshortname')";

或者:

$sql = "INSERT INTO clientele(`ID`,`ClientFullName`, `ClientShortName`) VALUES ('".$cid."','".$cname ."','".$cshortname."')";

但是你必须关心 SQL 注入问题!!!不要这样放到网上!

于 2013-08-26T15:56:42.130 回答