dongtuhe0506 2015-08-21 19:53
浏览 125
已采纳

sql命令INSERT INTO不起作用

I'm creating a website and I'm running into an error with inserting data from a form into my database (using phpMyAdmin). It is simply coming up with the self defined error 'Error', 'Sorry, your registration failed. Please go back and try again.'

I've looked at many other questions with the same issue but I cannot find an answer which corresponds to my problem.

<?php include "base.php"; ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">  

<div id="main">
 <?php

if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Email']))
{
    $PlanSize = mysql_real_escape_string($_POST['PlanSize']);
    $DatePaid = mysql_real_escape_string($_POST['DatePaid']);
    $AmountPaid = mysql_real_escape_string($_POST['AmountPaid']);
    $MonthUsage = mysql_real_escape_string($_POST['MonthUsage']);
     
        // $currentUser = mysql_query('SELECT * FROM User WHERE Email = "'. mysql_real_escape_string($_SESSION['Email']) . '"') or trigger_error(mysql_error());
     
        $currentUser = ($_SESSION['Email']);
        $registerquery = mysql_query("INSERT INTO Plan (PlanSize, DatePaid, AmountPaid, MonthUsage, PlanUserID) VALUES('".$PlanSize."','".$DatePaid."','".$AmountPaid."', '".$MonthUsage."','".$currentUser."')");
        if($registerquery)
        {
            echo "<h1>Success</h1>";
            echo "<p>Your account was successfully created. Please <a href=\"index.php\">click here to login</a>.</p>";
        }
        else
        {
            echo "<h1>Error</h1>";
            echo "<p>Sorry, your registration failed. Please go back and try again.</p>";    
        }       
     }

    ?>
    <form method="post" action="Plans.php">
    <table class="bill">
    <input type="hidden" name="data" value="true" />
    <fieldset>
        <p>
        <select name="PlanSize" id="PlanSize" placeholder="Plan Size"/>
            <option value="Small">Small</option>
            <option value="Medium">Medium</option>
            <option value="Large">Large</option>
            <option value="Extra Large">Extra Large</option>
        </select><p>

        <input type="text" name="DatePaid" id="DatePaid" placeholder="Date Paid (YYYY-MM-DD)"/><br />
        <input type="text" name="AmountPaid" id="AmountPaid" placeholder="Amount Paid"/><br />
        <input type="text" name="MonthUsage" id="MonthUsage" placeholder="Data Used"/><br />
        <input type="submit" name="Submit" id="Submit" value="Submit" />
    </fieldset>
    </table>
    </form>
  
</center>

</div>
</body></html>

Note: my base.php document is used to connect to the database and is fully functional; I have implemented other database data insertions using the base.php so I know it is not the root of the problem.

Many thanks in advance for any help

</div>
  • 写回答

3条回答 默认 最新

  • douyi8408 2015-08-21 20:02
    关注

    Give this a go:

    $registerquery = mysql_query("INSERT INTO `Plan` (`PlanSize`, `DatePaid`, `AmountPaid`, `MonthUsage`, `PlanUserID`) 
     VALUES('$PlanSize', '$DatePaid', '$AmountPaid', '$MonthUsage','$currentUser')");
    

    Moreover, Are you sure you're inserting the correct values to their respective columns? Is $currentUser really suppose to go inside PlanUserID column?

    After the discussion in the comments:

    Since you have the FK Constraint enable and it should work but let's check a few things:

    • The Parent and Child table columns should have same data types.
    • Make sure you don't have any values in your child table's column that do not exist in your parent table's column.
    • Both tables should have identical collation.
    • Both of your tables should be InnoDB.

    You can do so by:

    SHOW TABLE STATUS WHERE Name =  'table1';
    ALTER TABLE table1 ENGINE=InnoDB;
    

    And if all fails you can always take backup of your parent and child table first, then truncate child table and try to make a relation again.

    You can always study more at the similar questions available here: MySQL - Cannot add or update a child row: a foreign key constraint fails

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

报告相同问题?

悬赏问题

  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答