Dynadot .com transfer sale, only $10.12 with coupon code DYNA12, available while supplies last, ends October 31, 2026.

PHP post to MySQL

Namecheap AuctionsNamecheap Auctions
Namecheap AuctionsNamecheap Auctions
Spacemail by SpaceshipSpacemail by Spaceship
Watch

snike

Established Member
Impact
3
How do I make PHP echo the data in a MySQL database? How does PHP post into MySQL?
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
You have to use more functions from here http://www.php.net/mysql

mysql_connect - to connect to MySQL server
mysql_select_db - to select required database
mysql_query - to runa query on MySQL server.
 
0
•••
An example.

Database:
messages

Table:
messages
Code:
CREATE TABLE `messages` (
`id` INT(5) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
`email` VARCHAR(255) NOT NULL,
`message` LONGTEXT NOT NULL,
PRIMARY KEY (`id`)
) Type = MyISAM;

Example form + inserting data:
PHP:
<html>
<head>
<title>Example</title>
</head>

<body>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Your Name:<br />
<input type="text" name="name"><br /><br />
Your E-mail:<br />
<input type="text" name="email"><br /><br />
Your Message:<br />
<textarea rows="5" cols="40" name="message"></textarea><br /><br />
<input type="submit" name="submit" value="Submit">
</form>

<?php

//Connect to mysql
$db = mysql_connect('localhost', 'dbusername', 'dbpassword') or die("Couldn't connect:<br />".mysql_error());
mysql_select_db('messages', $db) or die("Couldn't select database:<br />".mysql_error());

if(isset($_POST['submit']))
{
  $name = trim(strip_tags(mysql_real_escape_string($_POST['name'])));
  $email = trim(strip_tags(mysql_real_escape_string($_POST['email'])));
  $messages = trim(strip_tags(mysql_real_escape_string($_POST['message'])));
  
  if(!empty($name) && !empty($email) && !empty($message))
  {
    $sql = mysql_query("INSERT INTO messages (id, name, email, message) VALUES('', '$name', '$email', '$message')");
    
    if($sql)
    {
      //Entered successfully
    }
    else
    {
      die(mysql_error());
    }
  }
}

?>
</body>
</html>

Displaying the data:
PHP:
<?php

//Connect
$db = mysql_connect('localhost', 'dbusername', 'dbpassword') or die("Couldn't connect:<br />".mysql_error());
mysql_select_db('messages', $db) or die("Couldn't select database:<br />".mysql_error());

$sql = mysql_query("SELECT * FROM messages") or die(mysql_error());

while($row = mysql_fetch_array($sql))
{
  echo 'Name: '.stripslashes($row['name']).'<br /><br />
  E-mail: '.stripslashes($row['email']).'<br /><br />
  Message: <br />'.stripslashes(nl2br($row['message'])).'<hr>';
}

?>
 
0
•••
Sv, you always beat me to it, although im not on np 24/7
 
0
•••
webmonkey said:
Sv, you always beat me to it, although im not on np 24/7
I'm pretty sure SV is some sort of AI robot, first of its kind ;) ;).
 
0
•••
:guilty:
 
0
•••
SecondVersion said:
lol, I knew it, it's speechless, its been found out :D
 
0
•••
DomainEasy: flexible LTO options. Close more domain deals with financing.

We're social

Escrow.com
Spaceship
Escrowly
CryptoExchange.com
Domain Recover

NamePros updates

New in Parking: updated way to act on many domains or inquiries at once
Your domain list and inquiry list now have a bar above and below the list. Check the items you want, then choose an action in the bar...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back