Jump to content

Archived

This topic is now archived and is closed to further replies.

aladiyat23

php mysql advice

Recommended Posts

ok i feel like an idiot.

i have a table created and a database waiting to be filled... this was created using phpmyadmin that my host provides.

ok... now for why i feel like an idiot... i cant find any tutorials or what to search for or where to look even for help on getting my contact form to write to this database. or am i ass backwards on this?

frustrated. :(

Share this post


Link to post
Share on other sites
Simplified way of inserting into a database

Form
[code]
<form action="form.php" method="POST">
My Name <input type="text" name="myName" />
<input type="submit" value="Submit" name="submit" />
</form>
[/code]

Script (in the same form as the form is you wish)
[code]
<?php
if(isset($_POST['submit'])) {
  // User submitted the form

  if(!empty($_POST['myName'])) {
    // Field 'myName' was not left empty
    $name = trim($_POST['myName']);
  }

  // Hopefully database connection has already been made

  // Having a string for the query statement is easier for troubleshooting bad data when the query buggers up
  $strqry = "INSERT INTO myDatabase (id, name, date_entered) VALUES (NULL, '{$name}', NOW())";

  // Execute the query with the string provided
  $query = mysql_query($strqry) or die("MySQL Error: <br />{$strqry} <br />".mysql_error());

  if(mysql_affected_rows > 0) {
    // Item was added successfully
    echo "Entry was added into the database";
  } else {
    // Item was not successful
    echo "There was an error when adding to the database.";
  }
}
?>
[/code]

Share this post


Link to post
Share on other sites
What errors are you getting?  Also, for tutorials check out [url=http://www.tizag.com]www.tizag.com[/url] - it helped me a ton when I first started using PHP...with SQL

Share this post


Link to post
Share on other sites

×

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.