0

这是我知道它很乱的代码,我正在重新学习,我的问题是当我转到页面时,查询立即运行我已经使用了该网站的一些建议,但到目前为止都没有奏效,我只是想获取输入的表单数据以插入到我的数据库表中。

<?php require_once('connection.php')?>

<?php //Post Params 
$F_name = $_POST['F_name'];  
$L_name = $_POST['L_name'];  
$Address1 = $_POST['Address1'];  
$Address2 = $_POST['Address2'];  
$City = $_POST['City'];  
$State = $_POST['State'];  
$Zip = $_POST['Zip'];  
$Email = $_POST['Email'];  
$P_number = $_POST['P_number'];  
$Contact_pref = $_POST['Contact_pref'];  

?>

<?php
//INSERT 
$query = " INSERT INTO Consumer_info ( F_name, L_name, Address1, Address2, City, State,      Zip, Email, P_number, Contact_pref )  VALUES ( '$F_name', '$L_name', '$Address1',     '$Address2', '$City', '$State', '$Zip', '$Email', '$P_number', '$Contact_pref' ) "; 
$result = mysql_query($query); 

if( $result )
{
echo 'Success';
}
else
{
echo 'You already have signed up thanks';
}

?>
<html>

<head>

<link rel="stylesheet" type="text/css" href="form_style.css" media="screen" />

<title>Database Test</title>

</head>

<body>
    <div id="wrapper">

<h1>Text Information form</h1>


<form id="form1" name="form1" action="index.php" method="post">
<label for="F_name">First Name</label><input type="text" name="F_name" id="F_name" />
<br class="clear" /> 
<label for="L_name">Last Name</label><input type="text" name="L_name" id="L_name" />
<br class="clear" /> 
<label for="Address1">Address</label><input type="text" name="Address1" id="Address1" />
<br class="clear" /> 
<label for="Address2">Unit/Apt/Suite</label><input type="text" name="Address2" id="Address2" />
<br class="clear" /> 
<label for="City">City</label><input type="text" name="City" id="City" />
<br class="clear" /> 
<label for="State">State</label><select name="State" id="State">
<option value="AL">AL</option>
<option value="AK">AK</option>
<option value="AZ">AZ</option>
<option value="AR">AR</option>
<option value="CA">CA</option>
<option value="CO">CO</option>
<option value="CT">CT</option>
<option value="DE">DE</option>
<option value="FL">FL</option>
<option value="GA">GA</option>
<option value="HI">HI</option>
<option value="ID">ID</option>
<option value="IL">IL</option>
<option value="IN">IN</option>
<option value="IA">IA</option>
<option value="KS">KS</option>
<option value="KY">KY</option>
<option value="LA">LA</option>
<option value="ME">ME</option>
<option value="MD">MD</option>
<option value="MA">MA</option>
<option value="MI">MI</option>
<option value="MN">MN</option>
<option value="MS">MS</option>
<option value="MO">MO</option>
<option value="MT">MT</option>
<option value="NE">NE</option>
<option value="NV">NV</option>
<option value="NH">NH</option>
<option value="NJ">NJ</option>
<option value="NM">NM</option>
<option value="NY">NY</option>
<option value="NC">NC</option>
<option value="ND">ND</option>
<option value="OH">OH</option>
<option value="OK">OK</option>
<option value="OR">OR</option>
<option value="PA">PA</option>
<option value="RI">RI</option>
<option value="SC">SC</option>
<option value="SD">SD</option>
<option value="TN">TN</option>
<option value="TX">TX</option>
<option value="UT">UT</option>
<option value="VT">VT</option>
<option value="VA">VA</option>
<option value="WA">WA</option>
<option value="WV">WV</option>
<option value="WI">WI</option>
<option value="WY">WY</option>
</select>
<br class="clear" /> 
<label for="Zip">Zip</label><input type="text" name="Zip" id="Zip" />
<br class="clear" /> 
<label for="Email">Email Address</label><input type="text" name="Email" id="Email" />
<br class="clear" /> 
<label for="P_number">Phone</label><input type="text" name="P_number" id="P_number" />
<br class="clear" /> 
<label for="Contact_pref">Contact Preferences</label><select name="Contact_pref" id="Contact_pref">
<option value="Email">Email</option>
<option value="Phone">Phone</option>
<option value="Mail">Mail</option>
<option value="None">None</option>
</select>
<br class="clear" /> 
<input type="hidden" name="Cust_id" id="Cust_id" />
<br class="clear" /> 
<input type="submit" name="submit" id="submit" value="Submit" />
<br class="clear" /> 
</form>

</div> <!-- End #wrapper -->

    </body>

</html>
4

4 回答 4

2

放:

if (isset($_POST['submit'])) {
    ...
}

围绕执行数据库查询的代码。

于 2013-09-29T04:49:16.507 回答
2

您必须检查表格是否已提交。最简单的方法是检查提交按钮的值是否已发布:

if( isset( $_POST['submit'] ) )
{
 // Your code here (MySQL INSERT, etc.)
}
于 2013-09-29T04:49:47.443 回答
0

您可以按如下方式更正您的代码:

<?php 
 require_once("connection.php");
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="form_style.css" media="screen" />
<title>Database Test</title>
</head>
<body>
//obviuosly you dont want to execute the query when the form is displayed.
<?php 
  $step=isset($_REQUEST['step'])?(int)$_REQUEST['step']:0;
  if($step==0):
?>
<div id="wrapper">
<h1>Text Information form</h1>
<form id="form1" name="form1" action="<?php echo basename(__FILE__).'?step=1';?>" method="post">
<label for="F_name">First Name</label><input type="text" name="F_name" id="F_name" />
<br class="clear" /> 
<label for="L_name">Last Name</label><input type="text" name="L_name" id="L_name" />
<br class="clear" /> 
<label for="Address1">Address</label><input type="text" name="Address1" id="Address1" />
<br class="clear" /> 
<label for="Address2">Unit/Apt/Suite</label><input type="text" name="Address2"      id="Address2" />
<br class="clear" /> 
<label for="City">City</label><input type="text" name="City" id="City" />
 <br class="clear" /> 
 <label for="State">State</label><select name="State" id="State">
 <option value="AL">AL</option>
 <option value="AK">AK</option>
 <option value="AZ">AZ</option>
 </select>
 <!--Other stuffs here -->
<input type="submit" name="submit" id="submit" value="Submit" />
<br class="clear" /> 
</form>
</div> <!-- End #wrapper -->
<?php
elseif($step==1):
 $F_name = isset($_POST['F_name'])?$_POST['F_name']:NULL;  
 $L_name = isset($_POST['L_name'])?$_POST['L_name']:NULL;
 //other similar stuffs 
 $res =  mysql_query("INSERT INTO Consumer_info ( F_name, L_name, Address1, Address2, City, State,      Zip, Email, P_number, Contact_pref )  VALUES ( '$F_name', '$L_name', '$Address1',     '$Address2', '$City', '$State', '$Zip', '$Email', '$P_number', '$Contact_pref' )");
 $retry = "<a herf='".basename(__FILE__)."?step=0/>Retry</a>";
 if($res)
 echo  "Record inserted successfully";
 else
 die("Cannot complete request, please ".$retry);
 endif;
?>
</body>
</html>
于 2013-09-29T06:13:59.327 回答
0
<?php
require_once('connection.php');
if (isset($_POST['submit2'])) {
    $a = $_POST['F_name'];
    $b = $_POST['L_name'];
    $c = $_POST['Address1'];
    $d = $_POST['Address2'];
    $e = $_POST['City'];
    $f = $_POST['State'];
    $g = $_POST['Zip'];
    $h = $_POST['Email'];
    $i = $_POST['P_number'];
    $j = $_POST['Contact_pref'];
    $k = " INSERT INTO Consumer_info ( F_name, L_name, Address1, Address2, City, State,      Zip, Email, P_number, Contact_pref )  VALUES ( '$a', '$b', '$c',     '$d', '$e', '$f', '$g', '$h', '$i', '$j' ) ";
    $l = mysql_query($k);
    if ($l) {
        echo 'Success';
    } else {
        echo 'You already have signed up thanks';
    }
}
?>
<html>
于 2013-09-29T04:56:01.137 回答