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

标签 php mysql forms jquery-mobile

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

我的问题是,每次刷新网络应用程序时,都会将一行插入到数据库的表中。在我单击/点击“提交”按钮之前,我显然不希望发生这种情况。我对网络应用程序开发比较陌生,希望得到有关我做错了什么的帮助。

关于如何在多页面布局上通过 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
?>

最佳答案

您需要分解插入逻辑并检查表单是否有效并准备好提交。

伪代码:

// 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 
    ...
}

关于php - 每次刷新 jquery 移动页面时,行都会插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15615106/

相关文章:

mysql - 查询多个 SQL 表以获取一种类型的对象

php - 能否在单个 MySQL 事务中将一个查询的结果用于另一个查询?

php - 按日期和余额排序银行移动

php - 使用 SimpleImage.php 类调整 PNG 透明度大小

php - 为什么这个 mySQL LEFT JOIN 不起作用?它可以是 WHERE 子句吗?

javascript - Angular:在 NgForm 的自定义输入组件中调用 markAsDirty()

php - 读取 urls-csv 并将它们插入到 mysql 中

mysql - "At most one record must be returned by this subquery"我是否搞砸了人际关系?

javascript - 避免动态表单中的 Javascript 跳跃元素

ruby-on-rails - 远程表单的 ajax 调用上的零星 ActionController::UnknownFormat