每次刷新jquery移动页面时,都会将行插入数据库


Row is inserted into database everytime I refresh the jquery mobile page

我正在制作一个jquery移动web应用程序,我正在使用多页面布局。当页面加载并在第二个div页面上连接到mySQL时,我有一个使用php文件的表单在提交时将表单中的值发送到mySQL数据库。

我的问题是,每次我刷新web应用程序,一行插入到我的数据库表。我显然不希望在点击Submit按钮之前发生这种情况。我是相对较新的web应用程序开发,并希望任何帮助,我做错了什么。

关于如何最好地通过php在多页面布局上插入mySQL数据库的建议将是伟大的。如果我需要显示更多的代码,请告诉我。
<!--Top of second page within multi page layout containing form-->
<!-- The POST A JOURNEY page where user creates a journey to be seen by others-->
    <div data-role="page" id="postAJourney" data-add-back-btn="true" data-back-btn-text="back" data-rel="back" data-direction="reverse" data-transition="flip" data-theme="b" >
        <div data-role="header"  data-position="fixed">
            <h1>Post A Journey</h1>
        </div>
        <div data-role="content">
            <form action="add_journey.php" method="post" id="postAJourneyForm">
<!--Top of multipage layout-->
<?php
require 'database.php';
?>

再次更新

<?php
//get the journey data
$radUserType = intval($_POST['radUserType']);
$pjFrom = $_POST['pjFrom'];
$pjTo = $_POST['pjTo'];
$radioJourneyType = intval($_POST['radioJourneyType']);
$departDate = $_POST['departDate'];
$returnDate = $_POST['returnDate'];
$textareanotes = $_POST['textAreaNotes'];
//check all values from $_POST
$canSubmitForm = false;
$isFormEmpty = false;
if (empty($radUserType) || empty($pjFrom) || empty($pjTo) || empty($radioJourneyType) || empty($departDate) || empty($returnDate) || empty($textareanotes))
{
$isFormEmpty = true;
}
//check for your data here
if(isset($radUserType) && isset($pjFrom) && isset($pjTo) && isset($radioJourneyType) &&     isset($departDate) && isset($returnDate) && isset($textareanotes))
{
$canSubmitForm = true;
}
$departTime = '11:12';
$returnTime = '11:16';
$seatcounter = '2';
//will only get executed if true
if ($canSubmitForm)
{
if ($isFormEmpty == false)
{
require_once('database.php');
$query = "INSERT INTO journey
    (from_destination,to_destination,journey_type,depart_date,depart_time,return_date,return_time,seats_available,journey_message,user_type)
        VALUES('$pjFrom','$pjTo','$radioJourneyType','$departDate','$departTime','$returnDate','$returnTime','$seatcounter','$textareanotes','$radUserType')";
$db->exec($query);
// Perform Query
//$result = mysql_query($query);
//if($result === true)
//{
//  $result = 'success';
//}
//else
//{
//  $result = 'insertion failure' . mysql_error();
//}
include('index.php');
}
}
else
{
$error = "Invalid product data. Check all fields and try again.";
include('error.php');
}
//Display the Product List page
?>

您需要打破Insert逻辑并检查表单是否有效并准备提交。

伪代码:

// check all values from $_POST
$canSubmitForm = false;
// check for your data here
// might want to verify type as well
if (isset($radUserType) && isset($pjFrom) && isset($pjTo) && isset($radioJourneyType))
{
    $canSubmitForm = true;
}
// will only get executed if true
if ($canSubmitForm)
{
    require_once('database.php');
    $query = "INSERT INTO journey
              (from_destination,to_destination,journey_type,depart_date,depart_time,return_date,return_ti      me,seats_available,journey_message,user_type)
              VALUES('$pjFrom','$pjTo','$radioJourneyType','$departDate','$departTime','$returnDate','$re    turnTime','$seatcounter','$textareanotes','$radUserType')";
    $db->exec($query);
    // Perform Query
    $result = mysql_query($query);
    // the rest of your code here 
    ...
}