PHP数据库:未在表

时间:2015-10-14 06:52:59

标签: php mysql

insert.php

<?php
mysql_connect("localhost","root",""); mysql_select_db("basic");
$name=$_POST['fname'];
$twait=$_POST['twait'];
$cprice=$_POST['cprice'];
$dprice=$_POST['dprice'];
$order= "INSERT INTO calculator
        (name,total_wt,crt_price,dollar_rate) VALUES
        ('$name','$twait','$cprice','$dprice')";

$result = mysql_query('$order');
echo "Done";
?>

HTML页面

<!DOCTYPE html>
<html>
<head>
<title>JN DIAMONDS</title>
</head>
<body>
<form align="center" method="POST" action="insert.php">
    <fieldset>
        <legend>Info</legend><br>
        <input type="text" name="fname" placeholder="Name"><br><br>
        <input type="text" name="twait" placeholder="Total Rough Weight"><br><br>
        <input type="text" name="cprice" placeholder="1 Carat Price"><br><br>
        <input type="text" name="dprice" placeholder="Dollar Rate"><br><br>
        <input type="submit" name="submit"value="Submit"><br>
    </fieldset>
</form>
</body>
</html>

4 个答案:

答案 0 :(得分:3)

$order是一个包含mysql字符串的变量。 当您将$order放在引号中,然后您没有将$order发送到mysql字符串中时,您实际上是在尝试执行查询'$order',这不是有效的mysql查询。

只需删除引号即可。

$result = mysql_query($order);

答案 1 :(得分:1)

已经指出了代码中的实际错误 mysql_ *扩展名为deprecated and will be removed in the upcoming version 7 of php;选择另一个api连接到你的MySQL服务器,例如PDO。使用prepared statements也可以处理最差sql injections

<?php
if ( !isset($_POST['fname'], $_POST['twait'], $_POST['cprice'], $_POST['dprice']) ) {
    trigger_error('missing POST parameter in '.var_export($_POST, true), E_USER_WARNING);
    echo '<html><head><title>...</title><body><h1>missing POST parameter</h1></body></html>';
}
else {
    $pdo = new PDO('mysql:host=localhost;dbname=basic;charset=utf8', 'root', '', array(
        PDO::ATTR_EMULATE_PREPARES=>false,
        PDO::MYSQL_ATTR_DIRECT_QUERY=>false,
        PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION
    ));
    $stmt = $pdo->prepare('
        INSERT INTO
            calculator
            (name,total_wt,crt_price,dollar_rate)
        VALUES
            (:fname,:twait,:cprice,:dprice)
    ');
    $stmt->execute(array(
        'fname'=>$_POST['fname'],
        'twait'=>$_POST['twait'],
        'cprice'=>$_POST['cprice'],
        'dprice'=>$_POST['dprice']
    ));
    echo "Done";
}

答案 2 :(得分:0)

请尝试此代码

<?php
    mysql_connect("localhost","root",""); mysql_select_db("basic");
    $name=$_POST['fname']; 
    $twait=$_POST['twait'];
    $cprice=$_POST['cprice']; 
    $dprice=$_POST['dprice'];
    $order= "INSERT INTO calculator
            (name,total_wt,crt_price,dollar_rate) VALUES
            ('$name','$twait','$cprice','$dprice')";
    $result = mysql_query($order);
    echo "Done";
    ?>

答案 3 :(得分:0)

使用mysqli而不是mysql。

$con = mysqli_connect('localhost', 'root', '', 'basic');
$name=$_POST['fname'];
$twait=$_POST['twait'];
$cprice=$_POST['cprice'];
$dprice=$_POST['dprice'];
$order= "INSERT INTO `calculator` (name,total_wt,crt_price,dollar_rate) 
VALUES ('".$name."','".$twait."','".$cprice."','".$dprice."')";

$result = mysqli_query($con,$order);
echo "Done";