PHP post to MySQL

Namecheap AuctionsNamecheap Auctions
Namecheap AuctionsNamecheap Auctions
SpaceshipSpaceship
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
Close
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
Close
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
Close
Sv, you always beat me to it, although im not on np 24/7
 
0
Close
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
Close
:guilty:
 
0
Close
SecondVersion said:
lol, I knew it, it's speechless, its been found out :D
 
0
Close
AIVikingsAIVikings

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Switch between accounts
You can now stay logged in to more than one NamePros account in the same browser. Click your avatar, then "Accounts in this browser", to...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back