PHP form not displaying anything in mySQL -
i trying figure out why php form doesn't put in data fields mysql database. have been trying figure out have come dead end.
my insert $sql works fine when hard coded values each field not when try use fields entered php form.
i dont't error when click on submit when check mysql see if added owner, nothing displays.
if can me fix this, appreciate it.
by way $sql insert statement correct quotes?
<head> <style> table, th, td { border: 1px solid black; border-collapse: collapse; } th, td { padding: 5px; } </style> </head> <body> <?php #index.php assignment 10 $page_title = 'assignment 10 marina database'; include('header.html'); require('dbconn.php'); echo '<h1> please enter following fields:</h1>'; if ($_server['request_method'] == 'post') { $ownernum=$_post['ownernum']; $lastname=$_post['lastname']; $firstname=$_post['firstname']; $address=$_post['address']; $city=$_post['city']; $state=$_post['state']; $zip=$_post['zip']; //echo test; try { $sql = "insert owner (ownernum, lastname, firstname, address, city, state, zip) values ('".$ownernum."', '".$lastname."', '".$firstname."', '".$address."', '".$city."', '".$state."', '".$zip."')"; //this works when hard coded /*$sql = "insert owner (ownernum, lastname, firstname, address, city, `state, zip) values ('xr34', 'patel', 'john', '342 picardy lane', 'wheeling', 'il', '60018')"; */` $conn->exec($sql); //echo $ownernum, $lastname, $firstname, $address, $city, $state, $zip; } catch (pdoexception $e) { echo 'error: '.$e->getmessage(); } //end catch } if (isset($_post['submit'])) { $stmt = $conn->prepare("select* owner"); $stmt->execute(); $result = $stmt->setfetchmode(pdo::fetch_assoc); echo "<table style='border: solid 1px black;'>"; echo "<tr><th>ownernum</th><th>lastname</th><th>firstname</th><th>address</th><th>city</th><th>state</th><th>zip</th></tr>"; class tablerows extends recursiveiteratoriterator { function __construct($it) { parent::__construct($it, self::leaves_only); } function current() { return "<td style='width:150px;border:1px solid black;'>" . parent::current(). "</td>"; } function beginchildren() { echo "<tr>"; } function endchildren() { echo "</tr>" . "\n"; } } foreach(new tablerows(new recursivearrayiterator($stmt->fetchall())) $k=>$v) { echo $v; } $conn = null; echo "</table>"; } ?> <form name="createowner" action="assignment10newowner.php" method="post"> <table style="width:100%"> <tr> <td>owner number:</td> <td><input type="text" name="ownernum"></td> </tr> <td>last name:</td> <td><input type="text" name="lastname"></td </tr> <tr> <td>first name:</td> <td><input type="text" name="firstname"></td </tr> <tr> <td>address:</td> <td><input type="text" name="address"></td </tr> <tr> <td>city:</td> <td><input type="text" name="city"></td </tr> <tr> <td>state:</td> <td><input type="text" name="state"></td </tr> <tr> <td>zip:</td> <td><input type="text" name="zip"></td </tr> </table> <br> <br> <input type="submit" value="submit"> </form> </body>
assign $_post values variable this
if (isset($_post['ownernum])) { $ownernum=$_post['ownernum']; $lastname=$_post['lastname']; $firstname=$_post['firstname']; $address=$_post['address']; $city=$_post['city']; $state=$_post['state']; $zip=$_post['zip']; try { $sql = "insert owner (ownernum, lastname, firstname, address, city, state, zip) values ('".$ownernum."', '".$lastname."', '".$firstname."', '".$address."', '".$city."', '".$state."', '".$zip."')"; $conn->exec($sql); $select_owner = "select * owner"; ?> <table> <tr> <td>ownernum</td> <td>firstname</td> <td>lastname</td> <td>address</td> <td>city</td> </tr> <?php $conn->prepare($select_owner ); $result = $conn->fetchall(); if (count($result)) { foreach($result $owner){ ?> <tr> <td><?=$owner['ownernum'];?></td> <td><?=$owner['firstname'];?></td> <td><?=$owner['lastname'];?></td> <td><?=$owner['address'];?></td> <td><?=$owner['city'];?></td> </tr> <?php } } else { ?> <tr> <td colspan="5">no onwers found</td> </tr> <?php } unset($result); unset($select_owner); unset($conn); } catch (pdoexception $e) { echo 'error: '.$e->getmessage(); } //end catch }
Comments
Post a Comment