Jump to content

Insert into database question


11Tami

Recommended Posts

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.

 

 

Link to comment
Share on other sites

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();}} 
?> 

Link to comment
Share on other sites

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>';

}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.