PHP修改数据库问题
<?php$host="localhost";$user="root";$pass="";$db="phpdev";$mysqli=newmysqli($host,$us...
<?php
$host = "localhost";
$user = "root";
$pass = "";
$db = "phpdev";
$mysqli = new mysqli($host, $user, $pass, $db);
if (mysqli_connect_errno()) {
die("Unable to connect!");
}
$mysqli->query("SET NAMES 'gbk'");
if (isset($_POST['btnModify'])) {
$sql="UPDATE 'phpdev'.'symbols' SET 'country' = '$_POST[country]','animal' = '$_POST[animal]','cname '='$_POST[cname]' WHERE 'symbols'.'id'' ='$_POST[id]'";
$result=$mysqli->query(sql);
if ($result) {
echo "修改成功";
}else {
echo "修改失败";
}
}
$query = "SELECT * FROM symbols";
if ($result = $mysqli->query($query)) {
if ($result->num_rows > 0) {
echo "<table cellpadding=10 border=1>";
echo "<form method='POST' action=''>";
while($row = $result->fetch_array()) {
echo "<tr>";
echo "<td><input name='id' type='text' id='id' value='$row[0]' /></td>";
echo "<td><input name='country' type='text' id='country' value='$row[1]' /></td>";
echo "<td><input name='animal' type='text' id='animal' value='$row[2]' /></td>";
echo "<td><input name='cname ' type='text' id='cname ' value='$row[3]' /></td>";
echo "<td><input name='bthModify' type='submit' id='bthModify' value='修改' /></td>";
echo "</tr>";
}
echo "</form>";
echo "</table>";
}
$result->close();
}
?>
修改不了数据库内容,高手们帮忙看下是什么原因 展开
$host = "localhost";
$user = "root";
$pass = "";
$db = "phpdev";
$mysqli = new mysqli($host, $user, $pass, $db);
if (mysqli_connect_errno()) {
die("Unable to connect!");
}
$mysqli->query("SET NAMES 'gbk'");
if (isset($_POST['btnModify'])) {
$sql="UPDATE 'phpdev'.'symbols' SET 'country' = '$_POST[country]','animal' = '$_POST[animal]','cname '='$_POST[cname]' WHERE 'symbols'.'id'' ='$_POST[id]'";
$result=$mysqli->query(sql);
if ($result) {
echo "修改成功";
}else {
echo "修改失败";
}
}
$query = "SELECT * FROM symbols";
if ($result = $mysqli->query($query)) {
if ($result->num_rows > 0) {
echo "<table cellpadding=10 border=1>";
echo "<form method='POST' action=''>";
while($row = $result->fetch_array()) {
echo "<tr>";
echo "<td><input name='id' type='text' id='id' value='$row[0]' /></td>";
echo "<td><input name='country' type='text' id='country' value='$row[1]' /></td>";
echo "<td><input name='animal' type='text' id='animal' value='$row[2]' /></td>";
echo "<td><input name='cname ' type='text' id='cname ' value='$row[3]' /></td>";
echo "<td><input name='bthModify' type='submit' id='bthModify' value='修改' /></td>";
echo "</tr>";
}
echo "</form>";
echo "</table>";
}
$result->close();
}
?>
修改不了数据库内容,高手们帮忙看下是什么原因 展开
3个回答
展开全部
你的错误主要在这里
$sql="UPDATE symbols SET country = '$_POST[country]',animal = '$_POST[animal]',cname='$_POST[cname_]' WHERE symbols.id=$_POST[id]";
当存在中文字符时,中文字符用''包围,而英文字符不用,否则会出错的。
像这种symbols.id,句号.只是用于表示指向的,如果是你在单个表中可以把表名省去,就用id就行,只有在多表查询中才需要指明某个字段属于哪个表单。
还有你下面的 $result=$mysqli->query(sql);sql前面应该加一个$。
下面是我改的代码,经过测试可以方便使用:
有个比较怪异的地方是,$_POST[cname_],它在POST表单中自动添加为cname_,这个是var_dump($_POST)测试的得到的结果,var_dump函数用于查看变量类型以及变量值的。还有后面把表单处改为
echo "<form method='POST' action='{$_SERVER['PHP_SELF']}'>";
actio指向自身,而且将其放在输出循环中,字符集也改为了UTF8,header头先输出声明,否则在IE以为的浏览器中会出现乱码
<?php
header("Content-type: text/html;charset=UTF-8");
$host = "localhost";
$user = "root";
$pass = "";
$db = "phpdev";
$mysqli = new mysqli($host, $user, $pass, $db);
if (mysqli_connect_errno()) {
die("Unable to connect!");
}
$mysqli->query("SET NAMES 'utf8'");
if (isset($_POST['bthModify'])) {
$sql="UPDATE {$db}.symbols SET country = '$_POST[country]',animal = '$_POST[animal]',cname='$_POST[cname_]' WHERE symbols.id=$_POST[id]";
$result=$mysqli->query($sql);
// var_dump($sql);
if ($result) {
echo "修改成功";
}else {
echo "修改失败";
}
}
$query = "SELECT * FROM symbols";
if ($result = $mysqli->query($query)) {
if ($result->num_rows > 0) {
echo "<table cellpadding=10 border=1>";
while($row = $result->fetch_array()) {
echo "<form method='POST' action='{$_SERVER['PHP_SELF']}'>";
echo "<tr>";
echo "<td><input name='id' type='text' id='id' value='$row[0]' /></td>";
echo "<td><input name='country' type='text' id='country' value='$row[1]' /></td>";
echo "<td><input name='animal' type='text' id='animal' value='$row[2]' /></td>";
echo "<td><input name='cname ' type='text' id='cname' value='$row[3]' /></td>";
echo "<td><input name='bthModify' type='submit' id='bthModify' value='修改' /></td>";
echo "</tr>";
echo "</form>";
}
echo "</table>";
}
$result->close();
}
?>
$sql="UPDATE symbols SET country = '$_POST[country]',animal = '$_POST[animal]',cname='$_POST[cname_]' WHERE symbols.id=$_POST[id]";
当存在中文字符时,中文字符用''包围,而英文字符不用,否则会出错的。
像这种symbols.id,句号.只是用于表示指向的,如果是你在单个表中可以把表名省去,就用id就行,只有在多表查询中才需要指明某个字段属于哪个表单。
还有你下面的 $result=$mysqli->query(sql);sql前面应该加一个$。
下面是我改的代码,经过测试可以方便使用:
有个比较怪异的地方是,$_POST[cname_],它在POST表单中自动添加为cname_,这个是var_dump($_POST)测试的得到的结果,var_dump函数用于查看变量类型以及变量值的。还有后面把表单处改为
echo "<form method='POST' action='{$_SERVER['PHP_SELF']}'>";
actio指向自身,而且将其放在输出循环中,字符集也改为了UTF8,header头先输出声明,否则在IE以为的浏览器中会出现乱码
<?php
header("Content-type: text/html;charset=UTF-8");
$host = "localhost";
$user = "root";
$pass = "";
$db = "phpdev";
$mysqli = new mysqli($host, $user, $pass, $db);
if (mysqli_connect_errno()) {
die("Unable to connect!");
}
$mysqli->query("SET NAMES 'utf8'");
if (isset($_POST['bthModify'])) {
$sql="UPDATE {$db}.symbols SET country = '$_POST[country]',animal = '$_POST[animal]',cname='$_POST[cname_]' WHERE symbols.id=$_POST[id]";
$result=$mysqli->query($sql);
// var_dump($sql);
if ($result) {
echo "修改成功";
}else {
echo "修改失败";
}
}
$query = "SELECT * FROM symbols";
if ($result = $mysqli->query($query)) {
if ($result->num_rows > 0) {
echo "<table cellpadding=10 border=1>";
while($row = $result->fetch_array()) {
echo "<form method='POST' action='{$_SERVER['PHP_SELF']}'>";
echo "<tr>";
echo "<td><input name='id' type='text' id='id' value='$row[0]' /></td>";
echo "<td><input name='country' type='text' id='country' value='$row[1]' /></td>";
echo "<td><input name='animal' type='text' id='animal' value='$row[2]' /></td>";
echo "<td><input name='cname ' type='text' id='cname' value='$row[3]' /></td>";
echo "<td><input name='bthModify' type='submit' id='bthModify' value='修改' /></td>";
echo "</tr>";
echo "</form>";
}
echo "</table>";
}
$result->close();
}
?>
展开全部
<?php
$host = "localhost";
$user = "root";
$pass = "";
$db = "phpdev";
$mysqli = new mysqli($host, $user, $pass, $db);
if (mysqli_connect_errno()) {
die("Unable to connect!");
}
$mysqli->query("SET NAMES 'gbk'");
if (isset($_POST['btnModify'])) {
$sql="UPDATE symbols SET country= '$_POST[country]',animal= '$_POST[animal]',cname='$_POST[cname]' WHERE id='$_POST[id]'";
$result=$mysqli->query($sql);
if ($result) {
echo "修改成功";
}else {
echo "修改失败";
}
}
$query = "SELECT * FROM symbols";
if ($result = $mysqli->query($query)) {
if ($result->num_rows > 0) {
echo "<table cellpadding=10 border=1>";
echo "<form method='POST' action=''>";
while($row = $result->fetch_array()) {
echo "<tr>";
echo "<td><input name='id' type='text' id='id' value='$row[0]' /></td>";
echo "<td><input name='country' type='text' id='country' value='$row[1]' /></td>";
echo "<td><input name='animal' type='text' id='animal' value='$row[2]' /></td>";
echo "<td><input name='cname ' type='text' id='cname ' value='$row[3]' /></td>";
echo "<td><input name='bthModify' type='submit' id='bthModify' value='修改' /></td>";
echo "</tr>";
}
echo "</form>";
echo "</table>";
}
$result->close();
}
?>
$host = "localhost";
$user = "root";
$pass = "";
$db = "phpdev";
$mysqli = new mysqli($host, $user, $pass, $db);
if (mysqli_connect_errno()) {
die("Unable to connect!");
}
$mysqli->query("SET NAMES 'gbk'");
if (isset($_POST['btnModify'])) {
$sql="UPDATE symbols SET country= '$_POST[country]',animal= '$_POST[animal]',cname='$_POST[cname]' WHERE id='$_POST[id]'";
$result=$mysqli->query($sql);
if ($result) {
echo "修改成功";
}else {
echo "修改失败";
}
}
$query = "SELECT * FROM symbols";
if ($result = $mysqli->query($query)) {
if ($result->num_rows > 0) {
echo "<table cellpadding=10 border=1>";
echo "<form method='POST' action=''>";
while($row = $result->fetch_array()) {
echo "<tr>";
echo "<td><input name='id' type='text' id='id' value='$row[0]' /></td>";
echo "<td><input name='country' type='text' id='country' value='$row[1]' /></td>";
echo "<td><input name='animal' type='text' id='animal' value='$row[2]' /></td>";
echo "<td><input name='cname ' type='text' id='cname ' value='$row[3]' /></td>";
echo "<td><input name='bthModify' type='submit' id='bthModify' value='修改' /></td>";
echo "</tr>";
}
echo "</form>";
echo "</table>";
}
$result->close();
}
?>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
$_POST[id]
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询