dpxw7293 2017-04-20 19:27
浏览 132

php事件插入/表单没有进入数据库

I am using a self-posting form with an insert, and an include page for connecting to database. When i run the process on the website, all if/else statements show true(meaning the site confirms the entry). But when I check my phpMyAdmin database, nothing has been stored. I am confused because the website confirms everything as entered, but my database wont populate. Lines and chunks have been commented out for the sake of experimentation. here's my code:

<?php
session_start();
if(isset ($_SESSION['uname']) == "true")    //If this is a valid user allow access to this page
{
    if(isset($_POST["submit"]))
    {
        //The form has been submitted and needs to be processed

        include 'connectionAgain.php';  //connects to the database

        //Validate the form data here!

        //Get the name value pairs from the $_POST variable into PHP variables
        //uses the same name/value pairs from the form
        /*$event_id = $_POST[event_id];
        $event_name = $_POST[event_name];
        $event_description = $_POST[event_description];
        $event_presenter = $_POST[event_presenter];
        $event_date = $_POST[event_date];
        $event_time = $_POST[event_time];*/

        $sqlHardCode = "INSERT INTO nephilim42_341 . wdv341 (event_id, event_name, event_description, event_presenter, event_date, event_time) VALUES (?, ?, ?, ?, ?, ?);";
        //Create the SQL command string
        /*$sql = "INSERT INTO wdv341 (";
        $sql .= "event_id, ";
        $sql .= "event_name, ";
        $sql .= "event_description, ";
        $sql .= "event_presenter, ";
        $sql .= "event_date, ";
        $sql .= "event_time";   //Last column does NOT have a comma after it.
        $sql .= ") VALUES (?,?,?,?,?,?)";*/ //? Are placeholders for variables

        //Display the SQL command to see if it correctly formatted.
        echo "<p>$sql</p>";

        $query = $connection->prepare($sql);    //Prepares the query statement

        //Binds the parameters to the query.
        //s = string: i = integer: b = blob: d = double: (DATATYPES)
        $query->bind_param("ssssss",null, $event_name, $event_description, $event_presenter, $event_date, $event_time);

        //Run the SQL prepared statements
        if ( $query->execute() )
        {
            $sqlHardCode = "INSERT INTO nephilim42_341 . wdv341 (null, event_name, event_description, event_presenter, event_date, event_time) VALUES ( $event_name, $event_description, $event_presenter, $event_date, $event_time);";
            /*$sql = "INSERT INTO wdv341 (";
            $sql .= "event_id, ";
            $sql .= "event_name, ";
            $sql .= "event_description, ";
            $sql .= "event_presenter, ";
            $sql .= "event_date, ";
            $sql .= "event_time";   //Last column does NOT have a comma after it.
            $sql .= ") VALUES ($event_id,$event_name,$event_description,$event_presenter,$event_date,$event_time)";*/

            $message = "<h1>Your record has been successfully added to the database.</h1>";
            $message .= "<p>Please <a href='redirect.php'>view</a> your records.</p>";
        }
        else
        {
            $message = "<h1>You have encountered a problem.</h1>";
            $message .= "<h2 style='color:red'>" . mysqli_error($link) . "</h2>";   //remove this for production purposes
        }

        $query->close();
        $connection->close();   //closes the connection to the database once this page is complete.
    }// ends if submit
    else
    {
        header('location: redirect.php');
        //Form has not been seen by the user.  display the form
    }
}//end Valid User True
else
{
    //Invalid User attempting to access this page. Send person to Login Page
    //header('Location: loginPage.php');
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>WDV341 Into PHP  - Event Form</title>
<style>
    html {
        color: white;
    }

    body {
        background-color: black;
    }
</style>

</head>

<body>
<?php
if(isset($_POST["submit"]))
{
    //If the form was submitted display the INSERT result message
?>
    <h1><?php echo $message = 'Your record has been successfully entered into the database'; ?></h1>
    <h1><?php echo "Return to <a href='eventInsertForm.php'>Event Form</a>to enter more events";?></h1>

<?php
}//end if
else
{
    //Display the Form.  The user will add a new record\
    include 'redirect.php';
?>

<p>This is the input form that allows the user/customer to enter the information for an event. Once the form is submitted and validated it will call the addPresenters.php page. That page will pull the form data into the PHP and add a new record to the database.</p>
<form id="eventForm" name="eventForm" method="post" action="eventInsertForm.php">
  <p>Add a new Event</p>
  <p>Event Name:
    <input type="text" name="event_name" id="event_name" />
  </p>
    <p>Event Description:
    <textarea name="event_description" id="event_description" col = "45" rows = "5"></textarea>
  </p>
  <p>Event Presenter:
    <input type="text" name="event_presenter" id="event_presenter" />
  </p>
  <p>Event Date:
    <input type="text" name="event_date" id="event_date" />
  </p>
  <p>Event Time:
    <input type="text" name="event_time" id="event_time" />
  </p>
  <p>
    <input type="submit" name="submit" id="submit" value="Add Event" />
    <input type="reset" name="button2" id="button2" value="Clear Form" />
  </p>
</form>
<?php
}//end else
?>
</body>
</html>

and the include...

<?php
//  This file contains the PHP coding to connect to the database.  This file uses the mysqli or improved mysql commands.
//
//  Include this file in any page that needs to access the database.  Use the PHP include command before doing any database accesses
//

$hostname = "localhost";
$username = "";
$database = "";//the name of the database.  Usually the same as the username.
$password = "";

//Builds the connection object called $db and selects the desired database.
//You will need to use the $link variable in the mysqli_query() commands whenever you run a query against the database.
$link = mysqli_connect($hostname, $username, $password, $database); //$link is the connection object created by this command.

//$link is a doorway to hell!!!

//Check to make sure you properly connected to the database.  This is some sample logic more suitable to a production environment
if($link->connect_error)
{
    die("Connection Failed: " . $link->connect_error);
}
else
{
    echo "Connected Successfully";
}

//Alternative method of checking for a successful connection. 

$link = mysqli_connect($hostname,$username,$password,$database) or die("Error " . mysqli_error($link));
?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭
    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么