php 插入到表中不起作用并且通过php页面没有错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7137631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Insert into table not working and no error through php page
提问by LeoSam
I have this code to insert into a table. My issue with INSERT INTO categories is that its never inserting data into the table and there is no error. I am using almost the same query in code with a different table and there it's working. Any clue?
我有这个代码插入到表中。我对 INSERT INTO 类别的问题是它从不将数据插入表中并且没有错误。我在具有不同表的代码中使用几乎相同的查询,并且它正在工作。有什么线索吗?
<?php
$action = $_GET['action'] ;
if ($action=='question')
question();
elseif ($action=='categories')
categories();
function question() {
if ((isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true))
{
$include("db.php");
$category = $_POST['category'] ;
$subcategory = $_POST['subCategory'] ;
$question = $_POST['question'] ;
$answer = $_POST['answer'] ;
$query = "INSERT INTO faq (category,subcategory,question,answer)
VALUES('.$category.','.$subcategory.','.$question.','.$answer')";
$success = mysql_query($query);
if ($success)
{
echo '<a href="admin.php" >done >';
}
else
{
echo mysql_error();
}
}
}
function categories(){
if ( ! (isset($_SESSION['loggedin']) && ! $_SESSION['loggedin'] == true))
{
include("db.php");
$category = $_POST['category'] ;
$subcategory = $_POST['subCategory'] ;
$query = "INSERT INTO categories (category,subcategory)
VALUES( '$category' , '$subcategory')";
$success = mysql_query($query);
if ($success)
{
echo '<a href="admin.php" >done>';
}
else
{
echo mysql_error();
}
}
}
?>
采纳答案by Luke Stevenson
A few issues:
几个问题:
- If you are combining variables into a string, you can use the "." character to join them, or you can include variables within the string, so long as the string is wrapped in double quotation marks. In your code, you were doing both at once.
- You were not santising your database input.
- Your logic checks for the "categories" function were incorrect.
- Your hyperlink tags were missing the closing tags.
- 如果要将变量组合成字符串,则可以使用“.”。字符来连接它们,或者您可以在字符串中包含变量,只要字符串用双引号括起来即可。在您的代码中,您同时进行了两项操作。
- 你没有清理你的数据库输入。
- 您对“类别”功能的逻辑检查不正确。
- 您的超链接标签缺少结束标签。
See the amended code below.
请参阅下面的修改后的代码。
<?php
$action = $_GET['action'];
if( $action=='question' )
question();
elseif( $action=='categories' )
categories();
function question(){
if( isset( $_SESSION['loggedin'] ) && $_SESSION['loggedin'] == true ){
include( 'db.php' );
$category = mysql_real_escape_string( $_POST['category'] );
$subcategory = mysql_real_escape_string( $_POST['subCategory'] );
$question = mysql_real_escape_string( $_POST['question'] );
$answer = mysql_real_escape_string( $_POST['answer'] );
$query = "INSERT INTO faq ( category , subcategory , question , answer ) VALUES( '{$category}' , '{$subcategory}' , '{$question}' , '{$answer}' )";
echo "SQL Query to execute: $query"; # Debug Message
$success = mysql_query( $query );
if ( $success ){
echo '<a href="admin.php">done</a>';
}else{
echo mysql_error();
}
}
}
function categories(){
if( !( isset( $_SESSION['loggedin'] ) || $_SESSION['loggedin']==true ) ){
include( 'db.php' );
$category = mysql_real_escape_string( $_POST['category'] );
$subcategory = mysql_real_escape_string( $_POST['subCategory'] );
$query = "INSERT INTO categories ( category , subcategory ) VALUES ( '{$category}' , '{$subcategory}' )";
echo "SQL Query to execute: $query"; # Debug Message
$success = mysql_query( $query );
if( $success ){
echo '<a href="admin.php">done</a>';
}else{
echo mysql_error();
}
}
}
回答by Bojangles
First off, to help debugging, I'd put these two lines at the top of your scripts to show all the errors produced. Don't put these in a production environment, however.
首先,为了帮助调试,我将这两行放在脚本的顶部以显示产生的所有错误。但是,不要将它们放在生产环境中。
error_reporting(E_ALL);
ini_set('display_errors', '1');
Change
改变
$query = "INSERT INTO faq (category,subcategory,question,answer) VALUES('.$category.','.$subcategory.','.$question.','.$answer')";
To this:
对此:
$query = "INSERT INTO faq (category,subcategory,question,answer) VALUES('".$category."','".$subcategory."','".$question."','".$answer."')";
You have missed out a .
(dot) after $answer
; it was a syntax error, not a query error.
你错过了一个.
(点)之后$answer
;这是语法错误,而不是查询错误。
To make things a bit simpler, you can actually omit the dots completely:
为了让事情简单一点,你实际上可以完全省略点:
$query = "INSERT INTO faq (category,subcategory,question,answer) VALUES('$category','$subcategory','$question','$answer')";
Do be aware of SQL injection attacks; use mysql_real_escape_string()
to make your query safe(er)
请注意 SQL 注入攻击;用于mysql_real_escape_string()
使您的查询安全(呃)
Another issue might be your include file. Try changing
另一个问题可能是您的包含文件。尝试改变
include("db.php");
To
到
require("db.php");
This will fail if the include file can't be found. In this case, go fix!
如果找不到包含文件,这将失败。在这种情况下,去修复!
回答by Iztok Mravlja
I had the same problem, used or die(mysql_error());
and realized I wasn't doing addslashes($string)
on one of my variables. It had characters I needed to escape.
我遇到了同样的问题,使用or die(mysql_error());
并意识到我没有在addslashes($string)
我的一个变量上做。它有我需要逃脱的角色。
回答by sohaib tanveer
i have been stuck in this problem for a long time the only solution i found is
我被这个问题困扰了很长时间,我找到的唯一解决方案是
use mysqli instead of mysql because in newer versions of php these mysql has been deprecated for example
使用 mysqli 而不是 mysql 因为在较新版本的 php 中,例如这些 mysql 已被弃用
use the following methods
使用以下方法
and make sure keep the order correct dirst the variable e.g '$conn' and then database name in the method 'mysqli_select_db($conne,'checksum');'.
并确保保持顺序正确 dirst 变量,例如 '$conn',然后是方法 'mysqli_select_db($conne,'checksum');' 中的数据库名称。
similarly in 'mysqli_query($conne,$enter_command);' first variable and then the variable for query.
同样在 'mysqli_query($conne,$enter_command);' 第一个变量,然后是查询变量。
$conn=mysqli_connect($mysql_host,$mysql_user,$mysql_password);
mysqli_select_db($conne,'checksum'); // checksum is database name
mysqli_query($conne,$enter_command);
mysqli_close($conne);
also make sure to give spaces when entering database table columns
还要确保在输入数据库表列时给出空格
$enter_command = "INSERT INTO dbase (name , lastname , password) VALUES ('".$name."','".$lastname."','".$password."')";
you can also use this syntax
你也可以使用这个语法
$enter_command = "INSERT INTO dbase (name , lastname , password) VALUES ('{$name}','{$lastname}','{$password}')";
try avoiding sql injection
尽量避免 sql 注入
$gender = mysql_real_escape_string($_POST['gender']);
hope this will work
希望这会奏效
回答by Blenderboy
Your code is working just fine, The issue here is the database auto increment was not set. This can happen When auto increment is not set. you will not be able to insert additional records beyond one no matter how many times you run your script. If you are having this kind of issue, check to make sure auto increment is enabled.
您的代码运行良好,这里的问题是未设置数据库自动增量。未设置自动增量时,可能会发生这种情况。无论您运行脚本多少次,您都无法插入超过一个的其他记录。如果您遇到此类问题,请检查以确保启用了自动增量。
This is not really an error but the developer of mysql or php should give a warning when the user forgets to enable auto increment so the user has the option to fix it or ignore it.
这并不是真正的错误,但是当用户忘记启用自动增量时,mysql 或 php 的开发人员应该发出警告,以便用户可以选择修复它或忽略它。
回答by RBoek
I had the same mysterious situation. And worse, on a remote, identical, database the same code was working. The solution was to define a default value (any value) to all the database fields... Hope this helps.
我也有同样的神秘情况。更糟糕的是,在远程、相同的数据库上,相同的代码正在运行。解决方案是为所有数据库字段定义一个默认值(任何值)...希望这会有所帮助。