html - Post Data from One PHP File to Another Gives Error -
i have 2 php files, 1 file submits data second file update action mysql database.
below code file submits data
<?php $host="localhost"; // host name $username="root"; // mysql username $password=""; // mysql password $db_name="test"; // database name $tbl_name="test_mysql"; // table name $server_name="localhost"; // create connection $con = new mysqli($server_name, $username, $password, $db_name , 3306); if ($con->connect_error) { die("connection failed: " . $con->connect_error); } // value of id sent address bar $id=$_get['id']; // retrieve data database $sql="select * $tbl_name id='$id'"; $result = $con->query($sql); $rows = $result->fetch_assoc(); ?> <table width="400" border="0" cellspacing="1" cellpadding="0"> <tr> <form name="form1" method="post" action="update_ac.php"> <td> <table width="100%" border="0" cellspacing="1" cellpadding="0"> <tr> <td> </td> <td colspan="3"><strong>update data in mysql</strong> </td> </tr> <tr> <td align="center"> </td> <td align="center"> </td> <td align="center"> </td> <td align="center"> </td> </tr> <tr> <td align="center"> </td> <td align="center"><strong>name</strong></td> <td align="center"><strong>lastname</strong></td> <td align="center"><strong>email</strong></td> </tr> <tr> <td> </td> <td align="center"> <input name="name" type="text" id="name" value="<?php echo $rows['name']; ?>"> </td> <td align="center"> <input name="lastname" type="text" id="lastname" value="<?php echo $rows['lastname']; ?>" size="15"> </td> <td> <input name="email" type="text" id="email" value="<?php echo $rows['email']; ?>" size="15"> </td> </tr> <tr> <td> </td> <td> <input name="id" type="hidden" id="id" value="<?php echo $rows['id']; ?>"> </td> <td align="center"> <input type="submit" name="submit" value="submit"> </td> <td> </td> </tr> </table> </td> </form> </tr> </table> <?php // close connection $con->close(); ?>
the second file update action presented below
<?php $host="localhost"; // host name $username="root"; // mysql username $password=""; // mysql password $db_name="test"; // database name $tbl_name="test_mysql"; // table name $server_name="localhost"; // create connection $con = new mysqli($server_name, $username, $password, $db_name , 3306); if ($con->connect_error) { die("connection failed: " . $con->connect_error); } // update data in mysql database $sql="update $tbl_name set name='$name', lastname='$lastname', email='$email' id='$id'"; $result=$con->query($sql); // if updated. if($result){ echo "successful"; echo "<br>"; echo "<a href='list_records.php'>view result</a>"; } else { echo "error"; } ?>
the error page presented below.
an suggestions fix problem
well haven't set values yet that's why it's getting error. first must wrap second file check if has submitted form. set variables inside.
<?php if(isset($_post['submit'])) { $name = $_post['name']; $lastname = $_post['lastname']; $email = $_post['email']; $id = $_post['id']; // rest of code goes here }
Comments
Post a Comment