11Tami Posted August 8, 2007 Share Posted August 8, 2007 Hello, I am trying to have data entered into several tables in a database and not just one table only. Is there any way to do this? I know you can do this with columns. For instance using a form field and whatever someone entered into the form would be inserted into the table. Insert INTO table_name columnname WHERE columnname = '$formfieldentry'"; But can you do this with actual tables in the database and not just the table columns? So insert something into a table of a database, but only if a table name = something? Please let me know if this can be done, thank you very much. Quote Link to comment Share on other sites More sharing options...
11Tami Posted August 9, 2007 Author Share Posted August 9, 2007 Here's where I'm at, I'm trying to use a form and drop downs to send data to a database table named "computers" The errors say Undefined index: name Undefined index: category Undefined index: category found here: $name = $_POST['name'] ; $category = $_POST['category'] ; if ($_POST['category'] Isn't it reading these fields in the form? Can someone see why the php isn't reading these named form fields? Am I supposed to have some php in the form itelf? Thanks very, very, much. <?php ini_set('error_reporting', 8191); ini_set('display_startup_errors', 1); ini_set('display_errors', 1);{ ?> </span> <form action="<?php $_SERVER['PHP_SELF'] ;?>" method="post"> <table> <tbody> <tr> <td><input name="name" size="25"/></td> </tr> <tr> <td> <select name="category"> <option value="first">first</option> <option value="second">second</option> <option value="third">third</option> </select> </td> </tr> <tr> <td><input value="Add" type="submit" /></td> </tr> </tbody> </table> </form> <?php } $con = mysql_connect("","",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("", $con); $name = $_POST['name'] ; $category = $_POST['category'] ; if ($_POST['category'] == 'first'){ $sql="INSERT INTO computers (name,category) VALUES ('" . $_POST['name'] . "','" . $_POST['category'] . "')"; if (mysql_query($sql,$con)) {echo "row added";} else if (!mysql_query($sql,$con)) {mysql_error();}} ?> Quote Link to comment Share on other sites More sharing options...
dcp Posted August 9, 2007 Share Posted August 9, 2007 Part of your page seems to be missing, so I don't know if this is the right fix or not. Below I've created a conditional statement to check if the form's been submitted (and added ' name="submit" ' to your submit button). <?php //If the Form's been submitted if (isset($_POST['submit']) && $_POST['submit'] == "Add") { // connect to the db and insert data $con = mysql_connect("","",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("", $con); $name = $_POST['name'] ; $category = $_POST['category'] ; // not sure why this is here. What are you doing if category's not "first"? if ($_POST['category'] == 'first'){ $sql="INSERT INTO computers (name,category) VALUES ('" . $_POST['name'] . "','" . $_POST['category'] . "')"; if (mysql_query($sql,$con)) { echo "row added"; } else if (!mysql_query($sql,$con)) {mysql_error();} } } // otherwise print the form else { echo ' </span> <form action="'.$_SERVER['PHP_SELF'].'" method="post"> <table> <tbody> <tr> <td><input name="name" size="25"/></td> </tr> <tr> <td> <select name="category"> <option value="first">first</option> <option value="second">second</option> <option value="third">third</option> </select> </td> </tr> <tr> <td><input value="Add" type="submit" name="submit" /></td> </tr> </tbody> </table> </form>'; } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.