Jump to content

PHP database form not working


ChaosKnight

Recommended Posts

Hi,

can somebody see why this code does absolutely nothing?

 

<?php session_start();
  include("dbstuff.inc");
  $link = mysql_connect($host,$username,$password)
    if (!$link) 
      die("Couldn't connect to server");
  echo "Connected successfully";
  $select = mysql_select_db($db);
    if (!$select)
      die("couldn't connect to database");
  echo "Connected to database";
  $date = date("Y-m-d H:i:s");
  foreach ($_POST as $field => $value)
  {
    $$field = strip_tags(trim($value));
  }
  $sql = "INSERT INTO contact (name,email,heading,message,unread,date_created) VALUES ('$name','$email','$heading','$message','yes','$date')";
  mysql_query($sql,$link);
  mysql_close($link);
  header("Location: index.php?page=contact");
?>

 

It gets sent via a normal HTML post form.

 

When I tried it on localhost without the database lines and echoed the variables it  worked perfectly..

So I guess something is wrong with the database lines?

 

The database host, username, password and database name also gets returned as expected, so nothing is wrong with that.

 

The problem is that absolutely nothing else gets returned... The page is completely blank, it doesn't even execute the header("location: ...") command.

 

Oh, and the shared host that I'm on doesn't support mysqli, just for in case you wondered...

Link to comment
https://forums.phpfreaks.com/topic/198867-php-database-form-not-working/
Share on other sites

Messy code. Try this.

<?php session_start();
include("dbstuff.inc");

$link = mysql_connect($host,$username,$password)
if (!$link) { 
die("Couldn't connect to server");
} else {
echo "Connected successfully";
}

$select = mysql_select_db($db);
if (!$select) {
die("couldn't connect to database");
} else {
echo "Connected to database";
}

$date = date("Y-m-d H:i:s", time());

foreach ($_POST as $field => $value) {
    $$field = strip_tags(trim($value));
}

$sql = "INSERT INTO contact (name,email,heading,message,unread,date_created) VALUES ('$name','$email','$heading','$message','yes','$date')";
mysql_query($sql,$link) or die(mysql_error());

mysql_close($link);

header("Location: index.php?page=contact");
?>

Archived

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

×
×
  • 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.