无法在mysql中插入一行

时间:2016-06-27 17:55:44

标签: mysql php

[![在此输入图像说明] [1]] [1]我使用LAMP服务器和PHP脚本连接到现有的Mysql服务器(本地服务器,即host = localhost:3036)。

我使用

创建了一个名为depositor的表
$sql2="create table depositor(serial int not null auto_increment primary  key, customer_name varchar(50), account_number varchar(20));";
$retval2=mysql_query($sql2, $conn);

if(! $retval2)
{
die('Failed to create the depositor table' .mysql_error());
}
echo "Successfully created the depositor table\n";

现在执行这些行后,按照上述模式创建了存款人表。

现在,我尝试使用

在此表中插入一行
$sql4="insert into depositor (serial_number, customer_name, account_number) values (1,"Hayes","A-102");";

此代码以某种方式生成错误,并且不执行PHP脚本。

问题是当我使用命令行登录mysql服务器时,我能够在那里运行相同的查询(在CLI中)。

insert into depositor (serial_number, customer_name, account_number) values (1,"Hayes","A-102");

我错过了什么?

我甚至尝试过

insert into depositor (customer_name, account_number) values ("Hayes","A-102");

认为其中一个属性是auto_increment

上述查询在命令行中正常执行,但在以

形式放在PHP脚本中时则不会执行
$sql4="insert into depositor (customer_name, account_number) values ("Hayes","A-102");";

为什么输出中出现这种差异?

我也在发布源代码。

enter code here
<html>
<head>
<title>Challenge01</title></head>
<body>

<?php
$dbhost='localhost:3036';
$dbuser='root';
$dbpass='root';
$conn=mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn)
{
die('Failed to connect to the server</br>' . mysql_error());
}
echo "Connected to the server successfully</br>";

$sql1="create database bank";
$retval1=mysql_query($sql1, $conn);
if(! $retval1)
{
die('Failed to create the database bank</br>' .mysql_error());
}
echo "Database created successfullly</br>";
mysql_select_db('bank');
$sql2="create table depositor(serial int not null auto_increment primary  key, customer_name varchar(50), account_number varchar(20));";

$sql3="create table loan(serial int not null auto_increment primary key, loan_number varchar(10), branch_name varchar(50), amount int);";
$retval2=mysql_query($sql2, $conn);
$retval3=mysql_query($sql3, $conn);
if(! $retval2)
{
die('Failed to create the depositor table</br>' .mysql_error());
}
echo "Successfully created the depositor table</br>";

if(! $retval3)
{
die('Failed to create the loan table</br>' .mysql_error());
}
echo "Successfully created the loan table</br>";
echo "Entering data into the depositor table</br>";


//$sql4 = "insert into depositor ( customer_name, account_number) values ("Hayes","A-102");";

/*
$sql5="insert into depositor (customer_name, account_number) values  ("Johnson","A-101");";
$sql6="insert into depositor values(3,"Johnson","A-201");";
$sql7="insert into depositor values(4,"Jones","A-217");";
$sql8="insert into depositor values(5,"Lindsay","A-222");";
$sql9="insert into depositor values(6,"Smith","A-215");";
$sql10="insert into depositor values(7,"Turner","A-305");";
*/

/*
$retval4=mysql_query($sql4, $conn);
if(! $retval4)
{
die('Could not insert the item in the depositor table' . mysql_error());
}
echo "Data successfully entered\n";*/
/*
$retval5=mysql_query($sql4, $conn);
if(! $retval4)
{
die('Could not insert the item in the depositor table' . mysql_error());
}
echo "Data successfully entered\n";




echo "Now entering data into the loan table\n";
$sql11="insert into loan values(1,"L-11","Round Hill",900);";
$sql12="insert into loan values(2,"L-14","Downtown",1500);";
$sql13="insert into loan values(3,"L-15","Perryridge",1500);";
$sql14="insert into loan values(4,"L-16","Perryridge",1300);";
$sql15="insert into loan values(5,"L-17","Downtown"1000);";
$sql16="insert into loan values(6,"L-23","Redwood",2000);";
$sql17="insert into loan values(7,"L-93","Mianus",500);";

echo "Data successfullly entered\n";
*/


$sqla="select * from depositor;";
$sqlb="select * from loan;";
$retvala=mysql_query($sqla, $conn);
$retvalb=mysql_query($sqlb, $conn);
if(! $retvala)
{
die('Failed to display the depositor table</br>' . mysql_error());
}
echo "The Depositor table is shown above</br>";
if(! $retvalb)
{
die('Failed to display the loan table</br>' . mysql_error());
}
echo "The Loan table is as shown above</br>";
mysql_close($conn);
?>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

这可能属于dba.stackexchange.com,但我看到的第一个问题是您创建了一个名为serial的列,然后尝试将一个值添加到名为serial_number的列中。您的$sql4语句的问题是引用 - 您必须转义内部双引号或使用转义类型函数 - 请参阅http://php.net/manual/en/function.mysql-real-escape-string.php以获取一种建议的方法。