dpj83664
2015-08-10 03:16
浏览 55
已采纳

Php表单(使用PDO)插入其他表(外键)

I need some help, I am trying to insert into multiple tables using PDO - Can someone see what I am doing wrong - I am not getting a parse errors (nor did I set up an asset error):

Here is my form:

addcontact.php

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Add New Contact</title>
<link rel="stylesheet" href="css/table.css" type="text/css" />
</head>
 <body>
 <div class="CSS_Table_Example" style="width:500px;height:350px;">
          <center>

<form action="insert.php" method="post">
<p>
<td>      
    <tr><label for="ContactName">Contact Name:</label>
    <input type="text" name="ContactName" id="ContactName">
 </tr></p>
  <p>
   <tr> <label for="ContactTypeId">Contact Type:</label>

<select name="ContactTypeId">
<option value="1">Contact</option>
<option value="2">Organization</option>

</select>
</p>
<p>
<td>
       <tr> <label for="AddressTypeId">Address Type:</label>

<select name="AddressTypeId">
<option value="1">Home</option>
<option value="2">Office</option>
<option value="3">Other</option>

</select>
</p>
<p>
   <tr><label for="Address1">Address 1:</label>
    <input type="text" name="Address1" id="Address1">
</tr></p>
<p>
   <tr><label for="Address2">Address 2:</label>
    <input type="text" name="Address2" id="Address1">
</tr></p>
<p>
   <tr><label for="City">City:</label>
    <input type="text" name="City" id="Address1">
</tr></p>
   <tr> <label for="StateId">State:</label>

<select name="StateId">
<option value="1">Alabama</option>
<option value="2">Alaska</option>
<option value="3">Arizona</option>
<option value="4">Arkansas</option>
<option value="5">Califorina</option>
<option value="6">Colorado</option>
<option value="7">Connecticut</option>
<option value="8">Delaware</option>
<option value="9">District of Columbia</option>
<option value="10">Florida</option>
<option value="11">Georgia</option>
<option value="12">Hawaii</option>
<option value="13">Idaho</option>
<option value="14">Illinois</option>
<option value="15">Indiana</option>
<option value="16">Iowa</option>
<option value="17">Kansas</option>
<option value="18">Kentucky</option>
<option value="19">Louisana</option>
<option value="20">Maine</option>
<option value="21">Maryland</option>
<option value="22">Massachusetts</option>
<option value="23">Michigan</option>
<option value="24">Minnesota</option>
<option value="25">Mississippi</option>
<option value="26">Missouri</option>
<option value="27">Montana</option>
<option value="28">Nebraska</option>
<option value="29">Nevada</option>
<option value="30">New Hampshire</option>
<option value="31">New Jersey</option>
<option value="32">New Mexico</option>
<option value="33">New York</option>
<option value="34">North Carolina</option>
<option value="35">North Dakota</option>
<option value="36">Ohio</option>
<option value="37">Oklahoma</option>
<option value="38">Oregon</option>
<option value="39">Pennsylvania</option>
<option value="40">Rhode Island</option>
<option value="41">South Carolina</option>
<option value="42">South Dakota</option>
<option value="43">Tennessee</option>
<option value="44">Texas</option>
<option value="45">Utah</option>
<option value="46">Vermont</option>
<option value="47">Virginia</option>
<option value="48">Washington</option>
<option value="49">West Virginia</option>
<option value="50">Wisconsin</option>
<option value="51">Wyoming</option>
</select>

</tr> </p>    
<input type="submit" value="Add Record">
</tr></td>
</form>
 </table>
</body>
</html>

Here is insert.php

<?php

$servername = "localhost";

$username = "root";

$password = "root";

$dbname = "crm";

//making an array with the data received, to use as named placeholders   for INSERT by PDO.

$data = array('ContactName' => $_POST['ContactName'] , 'ContactTypeId' 
=> $_POST['ContactTypeId'],

'ContactId'=> $_POST['ContactId'],'AddressTypeId'=>  

$_POST['AddressTypeId'],'Address1'=>$_POST['Address1'],
 'Address2'=>$_POST['   
  Address2'],'City'=>$_POST['City'],'StateId'=>$_POST['StateId']); 
 try {
 // preparing database handle $dbh

 $dbh = new PDO("mysql:host=$servername;dbname=$dbname",

 $username,$password);
// set the PDO error mode to exception

$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$currentID = mysql_inserted_id();

// query with named placeholders to avoid sql injections

$query = "INSERT INTO Contacts (ContactName, ContactTypeId)      
VALUES(:ContactName, :ContactTypeId )";

 $query2= "INSERT INTO 
 Addresses(ContactId,AddressTypeId,Address1,Address2,City,StateId) 
VALUES(:$currentID,:AddressTypeId,:Address1,:Address2,:City,:StateId)";
//statement handle $sth
$sth = $dbh->prepare($query);
$sth->execute($data);
echo "New record created successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$dbh = null;
?>
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题