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
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");
?>

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.