MySQL PHP 插入问题
MySQL PHP INSERT issue
关于插入命令我有一个非常有趣的问题。
我有一个由两列组成的数据库,它们是 "id" 和 "note",id 是主键并自动递增。
但是
我无法向 "note" 插入任何内容,但我可以向 "id" 插入数字。
echo '<form method="POST">';
echo '<table>';
echo '<thead>';
echo '<tr>';
echo '<th class="genislik100">Yeni Not Ekle</th>';
echo '<th></th>';
echo '<th class="metinSag"></th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
echo '<tr>';
echo '<td>Yeni Not:</td>';
echo '<td><input type="text" id="note" name="not" /></td>';
echo '<td class="metinSag"></td>';
echo '</tr>';
echo '<tr>';
echo '<td><input type="submit" name="submit" Value="Yeni Notu Ekle!!" /></td>';
echo '<td></td>';
echo '<td class="metinSag"></td>';
echo '</tr>';
echo '</tbody>';
echo '</table>';
echo '<form>';
if (isset($_POST['submit'])){
if($_POST['note'] != ''){
$note=$_POST['note'];
$con=mysql_connect("localhost","root","") or die("ERROR!!");
mysql_select_db("db") or die("COULDN'T FIND IT!!") or die("COULDN'T FIND DB");
$sql="INSERT INTO meteksan_notlar (note) VALUES ('$note')";
mysql_query($sql,$con);
echo '<script>alert("OKAY!!");</script>';
}else
echo "<script>alert('FILL THE BLANK!!');</script>";
}
我收到 "OKAY" 警报,但是当我查看数据库时,"note" 列中没有任何内容。为什么 ??
请帮忙。谢谢
<input type="text" id="note" name="not" />
name="not" 应该是 name="note"
除了
中的错别字
<input type="text" id="note" name="not" />
应该读作
<input type="text" id="note" name="note" />
将您的代码更改为以下内容,以反映当我告诉您在评论中添加错误报告时您收到的弃用消息。
if (isset($_POST['submit'])){
if($_POST['note'] != ''){
$note=$_POST['note'];
$con=mysqli_connect("localhost","root","", "db")
or die("Error " . mysqli_error($con));
$sql="INSERT INTO meteksan_notlar (note) VALUES ('$note')";
mysqli_query($con, $sql) or die(mysqli_error($con));
echo '<script>alert("OKAY!!");</script>';
}else {
echo "<script>alert('FILL THE BLANK!!');</script>";
}
}
- 我必须指出,您目前的代码是对 SQL injection. Use
mysqli
with prepared statements, or PDO with prepared statements 开放的,它们更安全。
将 error reporting 添加到您的文件的顶部,这将有助于查找错误。
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
旁注:错误报告只能在试运行中进行,绝不能在生产中进行。
脚注:
您还可以:(并防止 SQL 注入)
$note = stripslashes($_POST['note']);
$note = mysqli_real_escape_string($con, $_POST['note']);
$sql="INSERT INTO meteksan_notlar (note) VALUES ('$note')";
$query = mysqli_query($con, $sql) or die(mysqli_error($con));
if($query){
echo '<script>alert("OKAY!!");</script>';
}
else{
echo "There was an error" . mysqli_error($con);
}
关于插入命令我有一个非常有趣的问题。
我有一个由两列组成的数据库,它们是 "id" 和 "note",id 是主键并自动递增。
但是
我无法向 "note" 插入任何内容,但我可以向 "id" 插入数字。
echo '<form method="POST">';
echo '<table>';
echo '<thead>';
echo '<tr>';
echo '<th class="genislik100">Yeni Not Ekle</th>';
echo '<th></th>';
echo '<th class="metinSag"></th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
echo '<tr>';
echo '<td>Yeni Not:</td>';
echo '<td><input type="text" id="note" name="not" /></td>';
echo '<td class="metinSag"></td>';
echo '</tr>';
echo '<tr>';
echo '<td><input type="submit" name="submit" Value="Yeni Notu Ekle!!" /></td>';
echo '<td></td>';
echo '<td class="metinSag"></td>';
echo '</tr>';
echo '</tbody>';
echo '</table>';
echo '<form>';
if (isset($_POST['submit'])){
if($_POST['note'] != ''){
$note=$_POST['note'];
$con=mysql_connect("localhost","root","") or die("ERROR!!");
mysql_select_db("db") or die("COULDN'T FIND IT!!") or die("COULDN'T FIND DB");
$sql="INSERT INTO meteksan_notlar (note) VALUES ('$note')";
mysql_query($sql,$con);
echo '<script>alert("OKAY!!");</script>';
}else
echo "<script>alert('FILL THE BLANK!!');</script>";
}
我收到 "OKAY" 警报,但是当我查看数据库时,"note" 列中没有任何内容。为什么 ??
请帮忙。谢谢
<input type="text" id="note" name="not" />
name="not" 应该是 name="note"
除了
中的错别字<input type="text" id="note" name="not" />
应该读作
<input type="text" id="note" name="note" />
将您的代码更改为以下内容,以反映当我告诉您在评论中添加错误报告时您收到的弃用消息。
if (isset($_POST['submit'])){
if($_POST['note'] != ''){
$note=$_POST['note'];
$con=mysqli_connect("localhost","root","", "db")
or die("Error " . mysqli_error($con));
$sql="INSERT INTO meteksan_notlar (note) VALUES ('$note')";
mysqli_query($con, $sql) or die(mysqli_error($con));
echo '<script>alert("OKAY!!");</script>';
}else {
echo "<script>alert('FILL THE BLANK!!');</script>";
}
}
- 我必须指出,您目前的代码是对 SQL injection. Use
mysqli
with prepared statements, or PDO with prepared statements 开放的,它们更安全。
将 error reporting 添加到您的文件的顶部,这将有助于查找错误。
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
旁注:错误报告只能在试运行中进行,绝不能在生产中进行。
脚注:
您还可以:(并防止 SQL 注入)
$note = stripslashes($_POST['note']);
$note = mysqli_real_escape_string($con, $_POST['note']);
$sql="INSERT INTO meteksan_notlar (note) VALUES ('$note')";
$query = mysqli_query($con, $sql) or die(mysqli_error($con));
if($query){
echo '<script>alert("OKAY!!");</script>';
}
else{
echo "There was an error" . mysqli_error($con);
}