Jump to content

[SOLVED] Writing to MySQL


Recommended Posts

I'm attempting to write data from a form into a MySQL database. The script runs without errors and I am connecting to the db, but the data does not appear in the db. Can anyone tell me if I have written the script incorrectly? In the script below I have replaced my username and password with "user" and "password".

 

<?php

$wage = doubleval($_POST['Wage']);
$type = addslashes($_POST['TypeJob']);
$description = addslashes($_POST['Description']);
$comp_name = addslashes($_POST['Company Name']);
$contact = addslashes($_POST['Contact']);
$addresss = addslashes($_POST['Address']);
$telephone = addslashes($_POST['Telephone']);
$email = addslashes($_POST['Email']);
$counter_file = "counter.cnt";
$pre_conf = "E-";
$date = date("m/d/y");

if (! ereg('[A-Za-z0-9_-]+\@[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+', $email))
{
header("Location: ../error.htm");
exit;
}

$count_file = fopen("$counter_file", "r");
while (!feof($count_file)) 
{
$number = fread($count_file, 1024);
}
$conf_no = "$pre_conf$number";
fclose($count_file);

$fileopen = fopen("$counter_file", "w");
flock($fileopen, LOCK_EX);
$new_number = ($number + 1);
fwrite($fileopen, "$new_number");
fclose($fileopen);

$dbh = mysql_connect("localhost", "user", "password");

if (!$dbh)
{
echo "Error: Could not connect to database. Please try again later.";
exit;
}

mysql_select_db ("bransone_classifieds");

$query = "insert into bransone_classifieds values
	('".$conf_no."','".$date."','".$wage."','".$type."','".$description."','".$comp_name."','".$contact."','".$address."','".							$telephone."','".$email."')";

mysql_query($query);

header("Location: ../submission thanks.htm");

?>

 

As always, thanks for the help.

Link to comment
https://forums.phpfreaks.com/topic/60119-solved-writing-to-mysql/
Share on other sites

Sorry about the lack of indentation,I'm quite new to PHP and am not sure where your are supposed to indent.

 

I have managed to figure out what was wrong, I listed the name of the db where it should have been the name of the table I was writing to. Thanks for the code suggestion, it did the trick.

Cool.

 

Indentation should occur within logical blocks of code. Something like...

 

<?php

  if ($expression) {
    echo "this is within the if logical block";
  } else {
    echo "this is within the else logical block";
  }

  if ($expression) {
    if ($expression2) {
      echo "this is even further in";
    }
  }

?>

 

Its kinda difficult to explain, but I hope you can see how it makes it much easier to see which } belong to which if.

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.