无法使用php将数据插入mysql

时间:2022-09-25 16:08:57

I have been trying for two days now to figure this one out. I copied verbatim from a tutorial and I still cant insert data into a table. here is my code with form

我现在已经尝试了两天来解决这个问题。我从教程中逐字复制,但我仍然无法将数据插入表中。这是我的表格代码

<font face="Verdana" size="2"> 
<form method="post" action="Manage_cust.php" >
Customer Name 
<font face="Verdana"> 
<input type="text" name="Company" size="50"></font>
<br>
Customer Type 
<font face="Verdana"> 
<select name="custType" size="1">
  <option>Non-Contract</option>
  <option>Contract</option>
</select></font>
<br>
Contract Hours 
<font face="Verdana"> 
<input type="text" name="contractHours" value="0"></font>
<br>
<font face="Verdana">
<input type="submit" name="dothis" value="Add Customer"></font>
</form>
</font> 
<font face="Verdana" size="2">

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

    $con = mysql_connect ("localhost","root","password");
    if (!$con){
    die ("Cannot Connect: " . mysql_error());
    }

    mysql_select_db("averyit_net",$con);

    $sql = "INSERT INTO cust_profile (Customer_Name, Customer_Type, Contract_Hours)   VALUES 
    ('$_POST[Company]','$_POST[custType]','$_POST[contractHours]')";

    mysql_query($sql, $con);

    print_r($sql);


    mysql_close($con);
    }
?>

This is my PHPmyadmin server info:

这是我的PHPmyadmin服务器信息:

Server: 127.0.0.1 via TCP/IP Software: MySQL Software version: 5.5.27 - MySQL Community Server (GPL) Protocol version: 10 User: root@localhost Server charset: UTF-8 Unicode (utf8)

Server:127.0.0.1 via TCP / IP Software:MySQL Software version:5.5.27 - MySQL Community Server(GPL)Protocol version:10 User:root @ localhost Server charset:UTF-8 Unicode(utf8)

PLEASE tell me why this wont work. when I run the site it puts the info in and it disappears when I push the submit button, but it does not go into the table. There are no error messages that show up. HELP

请告诉我为什么这不起作用。当我运行网站时,它会将信息输入并在我按下提交按钮时消失,但它不会进入表格。没有出现错误消息。救命

3 个解决方案

#1


0  

I have improved a little bit in your SQL statement, stored it in an array and this is to make sure your post data are really set, else it will throw a null value. Please always sanitize your input.

我在SQL语句中进行了一些改进,将其存储在一个数组中,这是为了确保你的帖子数据真的被设置,否则它将抛出一个空值。请始终清理您的输入。

in your Manage_cust.php:

在您的Manage_cust.php中:

<?php

if (isset($_POST['dothis'])) 
{

$con = mysql_connect ("localhost","root","password");
    if (!$con)
    {
        die ("Cannot Connect: " . mysql_error());
    }

mysql_select_db("averyit_net",$con);

$company = isset($_POST['Company'])?$_POST['Company']:NULL;
$custype = isset($_POST['custType'])?$_POST['custType']:NULL;
$hours   = isset($_POST['contractHours'])?$_POST['contractHours']:NULL;

$sql = "INSERT INTO cust_profile(Customer_Name, 
                                 Customer_Type, 
                                 Contract_Hours)   
        VALUES('$company',
               '$custype',
               '$hours')
       ";

       mysql_query($sql, $con);

       mysql_close($con);
}
?>

#2


0  

First of all, don't use font tags...ever

首先,不要使用字体标签......

Secondly, because of this line:

其次,因为这一行:

if (isset($_POST['dothis'])) {

It looks like your HTML and PHP are combined into one script? In which case, you'll need to change the action on the form to something like this:

看起来您的HTML和PHP合并为一个脚本?在这种情况下,您需要将表单上的操作更改为以下内容:

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >

Plus, you can kill a bad connection in one line:

另外,你可以在一行中杀死一个错误的连接:

$con = mysql_connect("localhost","root","password") or die("I died, sorry." . mysql_error() );

Check your posts with isset() and then assign values to variables.

使用isset()检查帖子,然后为变量赋值。

var $company;
if(isset($_POST['Company']) {
    $company = $_POST['Company'];
} else {
    $company = null;
}
//so on and so forth for the other fields

Or use ternary operators

或者使用三元运算符

Also, using the original mysql PHP API is usually a bad choice. It's even mentioned in the PHP manual for the API

此外,使用原始的mysql PHP API通常是一个糟糕的选择。它甚至在API的PHP手册中提到过

Always better to go with mysqli or PDO so let's convert that:

总是更好地使用mysqli或PDO,所以让我们转换它:

//your connection
$conn = mysqli_connect("localhost","username","password","averyit_net");

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}


$sql = "INSERT INTO cust_profile (Customer_Name, Customer_Type, Contract_Hours) 
        VALUES ($company,$custType,$contractHours)";
            //  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            //  Assuming you set these

$stmt = mysqli_prepare($conn, $sql);
$stmt->execute();
$stmt->close();

Someone tell me if this is wrong, so I can correct it. I haven't used mysqli in a while.

有人告诉我这是不对的,所以我可以纠正它。我有一段时间没有使用过mysqli。

#3


0  

Change the $sql to this:

将$ sql更改为:

$sql = "INSERT INTO cust_profile (Customer_Name, Customer_Type, Contract_Hours)   VALUES ('".$_POST[Company]."','".$_POST[custType]."','".$_POST[contractHours]."')

#1


0  

I have improved a little bit in your SQL statement, stored it in an array and this is to make sure your post data are really set, else it will throw a null value. Please always sanitize your input.

我在SQL语句中进行了一些改进,将其存储在一个数组中,这是为了确保你的帖子数据真的被设置,否则它将抛出一个空值。请始终清理您的输入。

in your Manage_cust.php:

在您的Manage_cust.php中:

<?php

if (isset($_POST['dothis'])) 
{

$con = mysql_connect ("localhost","root","password");
    if (!$con)
    {
        die ("Cannot Connect: " . mysql_error());
    }

mysql_select_db("averyit_net",$con);

$company = isset($_POST['Company'])?$_POST['Company']:NULL;
$custype = isset($_POST['custType'])?$_POST['custType']:NULL;
$hours   = isset($_POST['contractHours'])?$_POST['contractHours']:NULL;

$sql = "INSERT INTO cust_profile(Customer_Name, 
                                 Customer_Type, 
                                 Contract_Hours)   
        VALUES('$company',
               '$custype',
               '$hours')
       ";

       mysql_query($sql, $con);

       mysql_close($con);
}
?>

#2


0  

First of all, don't use font tags...ever

首先,不要使用字体标签......

Secondly, because of this line:

其次,因为这一行:

if (isset($_POST['dothis'])) {

It looks like your HTML and PHP are combined into one script? In which case, you'll need to change the action on the form to something like this:

看起来您的HTML和PHP合并为一个脚本?在这种情况下,您需要将表单上的操作更改为以下内容:

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >

Plus, you can kill a bad connection in one line:

另外,你可以在一行中杀死一个错误的连接:

$con = mysql_connect("localhost","root","password") or die("I died, sorry." . mysql_error() );

Check your posts with isset() and then assign values to variables.

使用isset()检查帖子,然后为变量赋值。

var $company;
if(isset($_POST['Company']) {
    $company = $_POST['Company'];
} else {
    $company = null;
}
//so on and so forth for the other fields

Or use ternary operators

或者使用三元运算符

Also, using the original mysql PHP API is usually a bad choice. It's even mentioned in the PHP manual for the API

此外,使用原始的mysql PHP API通常是一个糟糕的选择。它甚至在API的PHP手册中提到过

Always better to go with mysqli or PDO so let's convert that:

总是更好地使用mysqli或PDO,所以让我们转换它:

//your connection
$conn = mysqli_connect("localhost","username","password","averyit_net");

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}


$sql = "INSERT INTO cust_profile (Customer_Name, Customer_Type, Contract_Hours) 
        VALUES ($company,$custType,$contractHours)";
            //  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            //  Assuming you set these

$stmt = mysqli_prepare($conn, $sql);
$stmt->execute();
$stmt->close();

Someone tell me if this is wrong, so I can correct it. I haven't used mysqli in a while.

有人告诉我这是不对的,所以我可以纠正它。我有一段时间没有使用过mysqli。

#3


0  

Change the $sql to this:

将$ sql更改为:

$sql = "INSERT INTO cust_profile (Customer_Name, Customer_Type, Contract_Hours)   VALUES ('".$_POST[Company]."','".$_POST[custType]."','".$_POST[contractHours]."')