Jump to content

Script won't insert to DB


Username:

Recommended Posts

I've used this on like three other scripts I don't know why it won't insert now  :shrug:

 

<?php
date_default_timezone_set('America/Los_Angeles');
$timestamp = date('H:m:s m.d');  
$admin = '24.68.214.97';
$visitor_ip = $_SERVER['REMOTE_ADDR'];
$host="mysql"; // Host name
$username="15557_test"; // Mysql username
$password="**********"; // Mysql password
$db_name="15557_test"; // Database name
$tbl="announce"; // Table name
mysql_connect("$host", "$username", "$password")or die("cannot connect to server");
mysql_select_db("$db_name")or die("cannot select DB"); 
$body = $_POST["body"];

if ($visitor_ip == $admin) {
mysql_query("INSERT INTO $tbl (body, date) VALUES ('$body', '$timestamp')"); //right here, i can echo $body and see what I wrote, but I can't insert into $tbl
}
else {
die("no");
}
?>
<meta http-equiv="REFRESH" content="0;url=http://testchan.dcfilms.org/board-b.php">

Link to comment
https://forums.phpfreaks.com/topic/214013-script-wont-insert-to-db/
Share on other sites

Either:

 

1) The IP does not match (they can change you know)

 

OR

 

2) The query is failing.

 

Try the following:

<?php
date_default_timezone_set('America/Los_Angeles');
$timestamp = date('H:m:s m.d');  
$admin = '24.68.214.97';
$visitor_ip = $_SERVER['REMOTE_ADDR'];
$host = "mysql"; // Host name
$username = "15557_test"; // Mysql username
$password = "**********"; // Mysql password
$db_name = "15557_test"; // Database name
$tbl = "announce"; // Table name
mysql_connect($host, $username, $password)or die("cannot connect to server");
mysql_select_db("$db_name")or die("cannot select DB"); 
$body = $_POST["body"];

if ($visitor_ip == $admin)
{
    $query = "INSERT INTO {$tbl} (`body`, `date`) VALUES ('{$body}', '{$timestamp}')";
    mysql_query($query) ordie("Query: {$query}<br />Error:".mysql_error());
}
else
{
    die("no");
}
?>
<meta http-equiv="REFRESH" content="0;url=http://testchan.dcfilms.org/board-b.php">

starting with mjdamato code  do this:

 

add this lines

     ini_set("display_errors", "1");
    error_reporting(E_ALL);

immediately  after the <?php line

 

and fix what seems to be a typo error in this line

    mysql_query($query) ordie("Query: {$query}<br />Error:".mysql_error());

should be

    mysql_query($query) or die("Query: {$query}<br />Error:".mysql_error());

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.