Jump to content

MySQL Query(insert into db)


Unholy Prayer

Recommended Posts

Ok, I can't figure out what's wrong with this php form to add information into the database.  This is my table structure:
[IMG]http://img119.imageshack.us/img119/5787/phptablewe9.gif[/img]

This is my script:
[code]
<?php
require_once('config.php');

if(isset($_POST['submit']))
{
    $name = $_POST['name'];
  $url = $_POST['message'];

  mysql_query("INSERT INTO menu_links (name,url)
      values ('$name', '$url')");

  echo "The link was successfully added.";
}
echo "<table align='center' width='50%' cellspacing='1' cellpadding='1' border='0'>
<form action='addlink.php' method='POST'>
        <tr>
  <td align='center' colspan='2'><b>Add a Menu Link</b></td>
</tr><tr>
  <td align='right'>Link Name:</td>
  <td align='left'><input type='text' name='name' size='20'></td>
</tr><tr>
  <td align='right'>Link URL:</td>
  <td align='left'><input type='text' name='url' size='20'></td>
</tr><tr>
  <td align='center' colspan='2'><input type='submit' value='submit'></td>
</tr>
</form>
  </table>";
?>[/code]

I'm not getting any error messages, but the message that should come up with the submit button is hit doesn't come up and nothing is inserted into the database.
Link to comment
Share on other sites

Of course you are not getting any error messages. You didn't ask for them. Plus "name" may be a reserved name in mySQL. Try enclosing it withing back quotes.

Change your query line to this to see any possible errors:
[code]mysql_query("INSERT INTO menu_links (name,url)
      values ('$name', '$url')") or die(mysql_error()); [/code]
Link to comment
Share on other sites

OK in order to do this you would want to use:

[code]
<?php
echo "<form method='post' action='?do=do'>";
echo "<table cellspacing='5' cellpadding='0'>";
echo "<td><font color='#FF0000'>All Fields Required</font></td>";
echo "<tr><td>Name:</td><td><input type='text' name='name' value='$_name'></td></tr>";
echo "<tr><td>Url:</td><td><input type='text' name='url' value='$_url'></td></tr>";
echo "<tr><td></td><td><div align='right'><input type='submit' value='Submit'></div></td></tr>";
echo "</table></form>";

if($_GET['do'] == 'do')
{
$_name = $_POST['name'];
$_url = $_POST['url'];
srand((double)microtime()*99999999); 
$_id = rand(100000,99999999);

mysql_query ("insert into menu_links (id, url, name) values ('$_id', '$_url', '$_name')");
}
?>
[/code]

That should work please let me know how it comes out, works or not. Taken from my own script, modified though. Thanks.
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.