Jump to content

How to make a submission form into Database?


anon

Recommended Posts

Simple example:

<?
if(isset($_POST['name'])){
$name = $_POST['name'];

$query = "INSERT INTO `db_table` (`id`,`name`) VALUES ('','$name')";
$run = mysql_query($query);
}
?>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Sample Form</title>
</head>
<body>
<form name="sampleForm" id="sampleForm" action="index.php" method="post" enctype="multipart/form-data">
Name: <input type="text" size="20" id="name" name="name" />
<br />
<input type="submit" />
</form>
</body>
</html>

Link to comment
Share on other sites

Hold the lines. Let me explain the problem fully

 

I have a form with the following stuff

<form id="1" name="addurl" method="post" action="addurl.php">
  Name<br />
  <input type="text" name="name" id="textfield2" />
  
<p>
URL <br />
    <input type="text" name="URL" id="textfield" />
<p>Description<br />
  <textarea name="description" id="textarea" cols="45" rows="5"></textarea>
  <p>
    <input type="submit" name="add" id="button" value="Submit" />
    <br />
  </form>
  Thank you for adding your URL to our directory!
<p> </p>
</body>
</html>

 

Obviously that's the form bit. I need to create a DB that will have these values for one entry. Could i do it like that or what?

Link to comment
Share on other sites

Do you want a single input searched against the url, title, or description fields in a database table?  Or are you looking for results based off a given url, title, and description?  Basically, depending on what exactly you're trying to accomplish, you can either setup the query to parse those three fields for the given input, or concatenate the three fields into one and then perform a search off the concatenated string.  Can you give me more details on what exactly it is you are trying to do?

Link to comment
Share on other sites

??? okay. check it.  I want to have a form where someone adds their website. This will consist of the name; URL and description. This is then added to the DB. Then, when someone types a keyword into a "search box"; it checks the keyword against the entries (name; url and description) and displays the results like google. The reason i want the three things(name; URL and description) to be linked is so that if one of the values matches the query, it displays all of them (like a google result).

 

Do you get it?  :-\

Link to comment
Share on other sites

Got it,

 

When you create your database table make sure you create an id as a primary key that auto increments.  When a form is submitted, the data creates a new row in the database table, where the id is unique to that row.  When performing the search you'd use something like:

 

$query = "SELECT `id`,`name`,`url`,`description` FROM `db_table` WHERE `name` LIKE '%$search_str%' OR `url` LIKE '%$search_str%' OR `description` LIKE '%$search_str%'";
$run = mysql_query($query);
while($arr = mysql_fetch_assoc($run)){
extract($arr);
echo "<h2><a href=\"$url\">$name</a></h2>".
"<p>$description</p>";
}

 

This is obviously a very basic example, but I believe it accomplishes what you're requesting.

Link to comment
Share on other sites

Thanks, so i got the table. Adding values from the form would work like this right?

<?php
   
   // database information
   $host = 'localhost';      
   $user = 'username';
   $password = 'password';
   $dbName = 'databasename';

   // connect and select the database
   $conn = mysql_connect($host, $user, $password) or die(mysql_error());
   $db = mysql_select_db($dbName, $conn) or die(mysql_error());

   // insert new entry in the database if entry submitted
   if (isset($_POST['newEntry']) && trim($_POST['newEntry']) != '') {
      // for easier variable handling...
      $newEntry = $_POST['newEntry'];
       
      // insert new entry into database
      $sql = "insert into testTable (someField) values ('$newEntry')";
      $result = mysql_query($sql, $conn) or die(mysql_error());              
   } // end if new entry posted
   
   // select all the entries from the table
   $sql = "select someField from testTable";
   $result = mysql_query($sql, $conn) or die(mysql_error());

   // echo out the results to the screen
   while ($list = mysql_fetch_array($result)) {
      echo "{$list['someField']} <br>";
   } // end while

   // echo out the form to add a new entry
   echo <<<FORMENT
<br> Make a new entry: <br>
<form action = "{$_SERVER['PHP_SELF']}" method = "post">
   <input type = "text" name = "newEntry" maxlength="20" size = "10">
   <input type = "submit" value = "Add Entry">
</form>
FORMENT;

?> 

 

Link to comment
Share on other sites

In this previous code you gave (i think)

<?
if(isset($_POST['name'])){
$name = $_POST['name'];

$query = "INSERT INTO `db_table` (`id`,`name`) VALUES ('','$name')";
$run = mysql_query($query);
}
?>

 

Where would i put 'url' and 'description' to the script

Link to comment
Share on other sites

This is an insert statement ,not a select statement.  The statement should look like this:

 


<?
if(isset($_POST['name'])){
$name = $_POST['name'];

$query = "INSERT INTO `db_table` (`id`,`name`,`url`,`description`) VALUES ('','$name','$url','$description')";
$run = mysql_query($query);
}
?>

Link to comment
Share on other sites

But i want each value from the form (name; url and description) to go into the DB

 

<?
if(isset($_POST['name'])){
$name = $_POST['name'];

$query = "INSERT INTO `db_table` (`id`,`name`) VALUES ('','$name')";
$run = mysql_query($query);
}
?>

there's only $name, should there also be $url and $description?

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.