每次刷新jquery移动页面时都会将行插入数据库 [英] Row is inserted into database everytime I refresh the jquery mobile page
问题描述
我正在制作一个jquery移动网络应用程序,我正在使用多页面布局。我在页面加载时连接到mySQL,在第二个div页面上,我有一个使用php文件的表单在提交时将表单中的值发送到mySQL数据库。
我的问题是,当我点击/点击提交按钮时,即使表单元素中没有数据并重定向到我的localhost / share / add_journey.php,它也会插入到数据库中。我对网络应用程序开发比较陌生,并希望得到任何关于我做错的帮助。
关于如何最好地插入mySQL的建议数据库通过PHP在多页面布局上会很棒。如果我需要显示更多代码,请告诉我。
<! - - 包含表格的多页面布局中的第二页顶部 - - >
<! - POST A JOURNEY页面,用户创建一个可供他人看到的旅程 - >
< span class =code-keyword>< div data-role = page id = postAJourney dat a-add-back-btn = true data-back-btn-text = back data-rel = 返回 数据方向 = reverse 数据转换 = flip < span class =code-attribute> data-theme = b >
< div data-role = header data-position = 已修复 >
< ; h1 > 发布旅程< ; / h1 >
< / div >
< div data-role = content >
< 表单 action = < span class =code-keyword> add_journey.php method = 发布 id = postAJourneyForm > ;
<! - 多页面布局 - >
<? php
需要' database.php';
?>
更新
<?php
// 获取旅程数据
$ radUserType = intval($ _ POST [' radUserType']);
$ pjFrom = $ _POST [' pjFrom 跨度>];
$ pjTo = $ _POST [' pjTo 跨度>];
$ radioJourneyType = intval($ _ POST [' radioJourneyType'跨度>]);
$ departDate = $ _POST [' departDate 跨度>];
$ returnDate = $ _POST [' returnDate 跨度>];
$ textareanotes = $ _POST [' textAreaNotes 跨度>];
// 检查$ _POST的所有值
$ canSubmitForm = false;
$ isFormEmpty = false;
if (空($ pjFrom)||空($ pjTo))
{
$ isFormEmpty = true;
}
// 检查您的数据
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'跨度>;
// 只有在执行时才会执行
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);
include(' index.php');
}
}
else
{
$ error = 无效的产品数据。检查所有字段,然后重试。;
include(' error.php');
}
// 显示产品列表页面
?>
radUserType = intval(
_POST [' radUserType']);
pjFrom =
I am making a jquery mobile web app and I am using a multi page layout. I connect to mySQL when the page loads and on the second div page, I have a form which using a php file sends the values in the form to mySQL database when submitted.
My problem is, when I click/tap the submit button it inserts into the database even if there is no data in the form elements and redirects to my localhost/share/add_journey.php. I''m relatively new to web app development and would like any help as to what I''m doing wrong.
Advice on how best to insert to a mySQL database through php on a multi-page layout would be great. Please let me know if I need to show more code.
<!--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';
?>
Updated
<?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($pjFrom) || empty($pjTo))
{
$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);
include('index.php');
}
}
else
{
$error = "Invalid product data. Check all fields and try again.";
include('error.php');
}
//Display the Product List page
?>
radUserType = intval(
_POST['radUserType']);
pjFrom =
这篇关于每次刷新jquery移动页面时都会将行插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!