Jump to content

Recommended Posts

I am having some issues with this piece of code. i am trying to get my time sheets stored into my data base. So I have this insert.php that i am using to submit the time sheet form I wrote in html. The form works, but when I first tried to submit, it told me that I had a syntax error on line 3 having to do with proper syntax to use for

 

<?php

$con = mysql_connect("localhost","root","*******");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }

 

So I added a ";" after the "if (!$con)"

 

Then i tried to submit the querey again and I got the Could not Connect error.

 

Here is the code I am using to insert the form to mysql:

 

<?php

$con = mysql_connect("localhost","root","");

if (!$con);

  {

  die('Could not connect: ' . mysql_error());

  }

 

mysql_select_db("Time_Sheets", $con);

 

$sql="INSERT INTO employees (Date, FirstName, LastName, JobName, TimeIn, TimeOut, Lunch, TotalHours)

VALUES

('$_POST[date]','$_POST[firstname]','$_POST[lastname]','$_POST[jobname]',

'$_POST[timein]','$_POST[timeout]','$_POST[lunch]','$_POST[totalhours]',)";

 

if (!mysql_query($sql,$con))

  {

  die('Error: ' . mysql_error());

  }

echo "Time Sheet Updated";

 

mysql_close($con)

?>

 

I used the same config to create a database. It connected and said database created. I have compared the two but have not seen any discrepancies. Any help is much appreciated. I am stumped. Thank you in advance

 

 

Link to comment
https://forums.phpfreaks.com/topic/189552-wont-connect-to-data-base/
Share on other sites

Thank you.

 

    I have already verified mysql is running and working. I will recheck the connection information again. However, I got the error without the ";" after "if (!$con)" So why did the error go away when it was added? I am a bit of a newb here I appreciate it muchly. Thanks

 

What was the full error? There is nothing wrong with the first three lines of PHP code that you said was causing an error.

However this is in correct syntax. You should place a semi-colon after the if.

if (!$con);

 

That will cause an error or a blank screen. I think the error you saw was a MySQL error not a PHP error. Remove the semi colon from the if statement and post the error message you get in full.

I apologize, it is an SQL syntax error.

 

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 3

 

I imagine that it has to do with the info in line 2 describing the $con

 

I just cant figure this out though. I have used this very same code to connect for means of creating a data base. It worked. I open up dreamweaver, open the two files insert.php and create_db.php and flip between them back and forth. I see know differences.

 

could it maybe be syntax error in the $sql that is causing the issue?

You have an extra comma at the end of your query

$sql="INSERT INTO employees (Date, FirstName, LastName, JobName, TimeIn, TimeOut, Lunch, TotalHours)

VALUES

('$_POST[date]','$_POST[firstname]','$_POST[lastname]','$_POST[jobname]',

'$_POST[timein]','$_POST[timeout]','$_POST[lunch]','$_POST[totalhours]',)";

Remove that and the error will be resolved. However you should not place raw _POST data into your query, as you'll be prone to SQL injection attacks. You should pass your _POST variables through to mysql_real_escape_string to help prevent such attacks.

wildteen88

 

Thank you very much. that worked. I only have one more question since you have some knowledge in sql.

 

I was able to create a db, however the table was not created. I used this code

 

<?php

$con = mysql_connect("localhost","root","");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }

 

// Create database

if (mysql_query("CREATE DATABASE Time_Sheets",$con))

  {

  echo "Database created";

  }

else

  {

  echo "Error creating database: " . mysql_error();

  }

 

// Create table

mysql_select_db("Time_Sheets", $con);

$sql = "CREATE TABLE employees

(

LastNameID int NOT NULL AUTO_INCREMENT,

PRIMARY KEY(LastNameID),

Date DATE (YYYY-MM-DD),

FirstName varchar(15),

LastName varchar(20),

JobName varchar (30),

TimeIn TIME (HH:MM:SS),

TimeOut TIME (HH:MM:SS),

Lunch decimal(5,2)

TotalHours decimal(5,2)

)";

 

I dont know how to set up the field vlaues, and I imagine what i have is wrong. Could you help me with this. I just have to be able to create the database and the table for it with the fields already created.

 

I appreciate any help you can offer.

Thanks in advance.

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.