dongyao2022 2011-08-19 05:26
浏览 56
已采纳

无法在php脚本中执行sql INSERT查询(mysql_query)。 PHP / MySQL - 时间敏感

UPDATE: NOW RESOLVED - Thanks everyone!

Fix: I had a column named "referred_by" and in my code it's called "referred_by_id" - so it was trying to INSERT to a column that didn't exist -- once I fixed this, it decided to work!

I have limited time left to work on this project. The clock is ticking.

I'm trying to INSERT $php_variables into a TABLE called "clients".

I've been trying for hours to get this script to work, and I got it to work once, but then I realized I forgot a field, so I had to add another column to the TABLE and when I updated the script it stopped working. I reverted by but now it's still not working and I'm just frustrating myself too much.

<?php

error_reporting(E_ALL);
ini_set("display_errors", 1);

if (!isset($_COOKIE["user"]))
{
    header ("Location: ./login.php");
}

else
{
    include ("./source.php");
    echo $doctype;
}

$birthday = $birth_year . "-" . $birth_month . "-" . $birth_day;
$join_date = date("Y-m-d");

$error_type = 0;

$link = mysql_connect("SERVER", "USERNAME", "PASSWORD");

if (!$link)
{
    $error = "Cannot connect to MySQL.";
    $error_type = 1;
}

$select_db = mysql_select_db("DATABASE", $link);

if (!$select_db)
{
    $error = "Cannot connect to Database.";
    $error_type = 2;
}

if ($referred_by != "")
{
    $result = mysql_query("
    SELECT id FROM clients WHERE referral_code = $referred_by
    ");

    if (!$result)
    {
        $error = "Cannot find referral.";
        $error_type = 3;
    }

    while ($row = mysql_fetch_array($result))
    {
        $referred_by_id = $row['id'];
    }
}

else
{
    $referred_by_id = 0;
}

$first_name = mysql_real_escape_string($_POST['first_name']);
$last_name = mysql_real_escape_string($_POST['last_name']);
$birth_month = mysql_real_escape_string($_POST['birth_month']);
$birth_day = mysql_real_escape_string($_POST['birth_day']);
$birth_year = mysql_real_escape_string($_POST['birth_year']);
$email = mysql_real_escape_string($_POST['email']);
$address = mysql_real_escape_string($_POST['address']);
$city = mysql_real_escape_string($_POST['city']);
$state = mysql_real_escape_string($_POST['state']);
$zip_code = mysql_real_escape_string($_POST['zip_code']);
$phone_home = mysql_real_escape_string($_POST['phone_home']);
$phone_cell = mysql_real_escape_string($_POST['phone_cell']);
$referral_code = mysql_real_escape_string($_POST['referral_code']);
$referred_by = mysql_real_escape_string($_POST['referred_by']);
$organization = mysql_real_escape_string($_POST['organization']);
$gov_type = mysql_real_escape_string($_POST['gov_type']);
$gov_code = mysql_real_escape_string($_POST['gov_code']);

$test_query = mysql_query
("
INSERT INTO clients (first_name, last_name, birthday, join_date, email, address, city, state, zip_code,
phone_home, phone_cell, referral_code, referred_by_id, organization, gov_type, gov_code)
VALUES ('".$first_name."', '".$last_name."', '".$birthday."', '".$join_date."', '".$email."', '".$address."', '".$city."', '".$state."', '".$zip_code."',
'".$phone_home."', '".$phone_cell."', '".$referral_code."', '".$referred_by_id."', '".$organization."', '".$gov_type."', '".$gov_code."')
");

if (!$test_query)
{
    die(mysql_error($link));
}

if ($error_type > 0)
{
    $title_name = "Error";
}

if ($error_type == 0)
{
    $title_name = "Success";
}

?>


<html>
    <head>
        <title><?php echo $title . " - " . $title_name; ?></title>
        <?php echo $meta; ?>
        <?php echo $style; ?>
    </head>
    <body>
        <?php echo $logo; ?>
        <?php echo $sublogo; ?>
        <?php echo $nav; ?>
        <div id="content">
            <div id="main">

                <span class="event_title"><?php echo $title_name; ?></span><br><br>

                <?php

                if ($error_type == 0)
                {
                    echo "Client was added to the database successfully.";
                }

                else
                {
                    echo $error;
                }

                ?>

            </div>
            <?php echo $copyright ?>
        </div>
    </body>
</html>
  • 写回答

3条回答 默认 最新

  • dtotwai720621 2011-08-19 05:52
    关注

    You've an error on line 81:

    else
    {
        $referred_by_id = 0;
    }
    

    I don't see an IF construct before that, make the appropriate correction and run the script again.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改