Jump to content

Recommended Posts

God, I know this is so simple and yet I have spent all day to get where I am... !!! ERRRR!

 

So, I have this script pulling info from one table and inserting it depending on the Personal_ID that is entered in the form...

Well I am having a few problems.. one.. I get "Error:You have an error in your SQL syntax near '1' at line 1" and it wont display the rows that it should. and... if I refresh the page, it will enter the last personal_ID I typed in the box again in the database...

 

AS WELL I need the time and date when they clock in but my code is sooo not working....

 

Here is the whole code:

<html>
<head>
<title>Clock In</title>
</head>
<body>
<script language="JavaScript1.2">

function checknumber(){
var x=document.checknum.Personal_ID.value
var anum=/(^\d+$)|(^\d+\.\d+$)/
if (anum.test(x))
testresult=true
else{
alert("You must use a valid number. Do not enter your name!")
testresult=false
}
return (testresult)
}

</script>
<script>
function checkban(){
if (document.layers||document.all||document.getElementById)
return checknumber()
else
return true
}
</script>

<form action="clock.php" method="post" name=checknum onSubmit="return checkban()"><br/>
Employee Number:<br/> <input type="text" name="Personal_ID" />
<input type="hidden" value="<?php $b = time (); print date("m/d/y",$b); ?>  " name="Date">
<input type="hidden" value="<?php $b = time (); print date("g:i A",$b); ?>  " name="ClockTime">
<input type="submit" name="ClockType "value="Clock In">
</form>

<?php
$con = mysql_connect("cefdata","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db ('clocked');
$result = mysql_query("INSERT INTO clockers.clocked (Personal_ID, First_Name, Last_Name, Division)SELECT Personal_ID, First_Name, Last_Name, Division FROM masterdata.employees WHERE Personal_ID='{$_POST['Personal_ID']}'");
if (mysql_query($result))
{
  echo $row['First_Name'] . " " . $row['Last_Name'] . "";
  echo "1 record added";
}
else{ 
  die('Error:' .mysql_error().'');
}
;mysql_close($con)
?>

</body>
</html>

 

Can someone help me fix this?

 

Richard

Link to comment
https://forums.phpfreaks.com/topic/42039-going-mad-syntax-error/
Share on other sites

What are you trying to do with this query:

INSERT INTO clockers.clocked (Personal_ID, First_Name, Last_Name, Division)SELECT Personal_ID, First_Name, Last_Name, Division FROM masterdata.employees WHERE Personal_ID='{$_POST['Personal_ID']}

 

??? That's not valid at all

What I am trying to do is:

 

Using the form, I am supplied with a Personal_ID that will look up data from a master table and than copy the information over to a new table. It works other than the date and time.

 

Thanks for showing me that stray semicolon..I am new to programming with php so I am sure my query is funny to look at lol..

 

Anyone think they can help me straighten out my query?

 

Thanks Richard.

You need to divide it into two different queries. Also, you need to use mysql_fetch_array() to get the results (or mysql_result() or mysql_fetch_assoc()).

Here's how I think it should be:

 

<?php
$con = mysql_connect("cefdata","root","");

if (!$con)
die('Could not connect: ' . mysql_error());
mysql_select_db ('clocked');

$q1 = "INSERT INTO clockers.clocked (Personal_ID, First_Name, Last_Name, Division)";
$result1 = mysql_query($q1) or die("Error inserting: ".mysql_error());
if(mysql_affected_rows())
echo "1 record added<br>";

$q2 = "SELECT Personal_ID, First_Name, Last_Name, Division FROM masterdata.employees WHERE Personal_ID='{$_POST['Personal_ID']}'";
$result2 = mysql_query($q2) or die("Error selecting: ".mysql_error());
if (mysql_num_rows())
{
$row = mysql_fetch_array($result2);
echo $row['First_Name'] . " " . $row['Last_Name'] . "";
}

mysql_close($con);

?>

 

 

Orio.

The below code gives me the following error:

 

Error inserting: You have an error in your SQL syntax near '' at line 1

 

and no longer works at all.

 

Richard

 

<?php
$con = mysql_connect("cefdata","root","");

if (!$con)
die('Could not connect: ' . mysql_error());
mysql_select_db ('clocked');

$q1 = "INSERT INTO clockers.clocked (Personal_ID, First_Name, Last_Name, Division)";
$result1 = mysql_query($q1) or die("Error inserting: ".mysql_error());
if(mysql_affected_rows())
echo "1 record added<br>";

$q2 = "SELECT Personal_ID, First_Name, Last_Name, Division FROM masterdata.employees WHERE Personal_ID='{$_POST['Personal_ID']}'";
$result2 = mysql_query($q2) or die("Error selecting: ".mysql_error());
if (mysql_num_rows())
{
$row = mysql_fetch_array($result2);
echo $row['First_Name'] . " " . $row['Last_Name'] . "";
}

mysql_close($con);

?>

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.