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

Link to comment
Share on other sites

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.

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.