Jump to content

cannot insert into table


flemingmike

Recommended Posts

hello, anybody able to see what im doing wrong?

 

	 $id2=mysql_insert_id();
 $year=$_POST['y'];
 $yr=substr($year,-2);
 $mth=$_POST['m'];
 $init=$_POST['initial'];	 
 $jobnumber=$yr.$mth.'-'.$id2.$init;

 $query = "INSERT INTO jobs VALUES (
 '',
 '$id2',
 '$_POST[initial]',
 '$_POST[y]-$_POST[m]-$_POST[d]',date
 '$_POST[contact]',
 '$_POST[contactphone]',
 '$_POST[customer]',
 '$_POST[address]',
 '$_POST[city]',
 '$_POST[postal]',
 '$_POST[province]',
 '$_POST[description]'
 )";
 mysql_query($query) or die('Error, adding new job failed.  Check you fields and try again.');

 echo "You have successfully entered a new job.  The job number is $jobnumber";

Link to comment
Share on other sites

Two questions (for which I'm not sure of the answers without testing / researching more:

 

1) Can you use an associative array without quotes for the index?

 

//Is this valid?
$_POST[initial];
//Or must it be like this:
$_POST['initial'];
//Or even like this:
$_POST["initial"];

 

2) I don't think that you did this line properly:

'$_POST[y]-$_POST[m]-$_POST[d]',date

 

Remove the "date" part.

Link to comment
Share on other sites

ok, so ive changed it to this:

 

	 $id2=mysql_insert_id();
 $year=$_POST['y'];
 $yr=substr($year,-2);
 $mth=$_POST['m'];
 $init=$_POST['initial'];	 
 $jobnumber=$yr.$mth.'-'.$id2.$init;

 $sql = mysql_query("INSERT INTO members VALUES (
 NULL, 
 '$yr',
 '$init',
 '$_POST['y']-$_POST['m']-$_POST['d']',
 '$_POST['contact']',
 '$_POST['contactphone']',
 '$_POST['customer']',
 '$_POST['address']',
 '$_POST['city']',
 '$_POST['postal']',
 '$_POST['province']',
 '$_POST['description']'
 NULL, 
 NULL, 
 '1', 
 CURRENT_TIMESTAMP)");
 mysql_query($sql) or die('Error, adding new job failed.  Check you fields and try again.');

 

this still doesnt work

Link to comment
Share on other sites

Yes but you didn't put in curly braces as Pikachu2000 suggested. Go through the code and do that - because right now your code keeps breaking at every single quote you use.

 

Think of your quotes as levels. If you are in level 1 ("s) and use a single quote (') then you are in level 2. If you use another single quote, you break back into level 1.

(If that makes any sense to you)

So in the code you provided, you basically kept changing levels at the wrong places when in fact you wanted to go to a third level, of sorts, by using the curly braces.

Link to comment
Share on other sites

ok, i did all that, still no good.  here is my whole page.  maybe i have another screwup.

 

<?php
include 'config.php';

// After validating that form has been submitted . . .
array_map('trim', $_POST); // trim leading and trailing whitespace from all elements of $_POST array.

echo "<center>";
if($_POST['setupby'] == "Select...") {
     // validation fails, do something
 echo "You need to select your name from the Setup By field.";
 echo "<br /><br /><br /><br /><a href='#' onclick='javascript:history.go(-1);'>Go Back</a>";
 } elseif( empty($_POST['contact']) ) {
     // validation fails, do something
 echo "You need to enter a Contact Name.";
 echo "<br /><br /><br /><br /><a href='#' onclick='javascript:history.go(-1);'>Go Back</a>";
 } elseif($_POST['customer'] == "Select...") {
     // validation fails, do something
 echo "You need to select a Customer name.";
 echo "<br />If the customer didn't exist in the drop down list, they can be added <a href='newcompany.php'>Here</a>";
 echo "<br /><br /><br /><br /><a href='#' onclick='javascript:history.go(-1);'>Go Back</a>";
 } elseif($_POST['address'] == "Address") {
     // validation fails, do something
 echo "You need to enter an Address.";
 echo "<br /><br /><br /><br /><a href='#' onclick='javascript:history.go(-1);'>Go Back</a>";
 } elseif( empty($_POST['address']) ) {
     // validation fails, do something
 echo "You need to enter an Address.";
 echo "<br /><br /><br /><br /><a href='#' onclick='javascript:history.go(-1);'>Go Back</a>";
 } elseif($_POST['city'] == "City") {
     // validation fails, do something
 echo "You need to enter a City.";
 echo "<br /><br /><br /><br /><a href='#' onclick='javascript:history.go(-1);'>Go Back</a>";
 } elseif( empty($_POST['city']) ) {
     // validation fails, do something
 echo "You need to enter a City.";
 echo "<br /><br /><br /><br /><a href='#' onclick='javascript:history.go(-1);'>Go Back</a>";
 } elseif( empty($_POST['description']) ) {
     // validation fails, do something
 echo "You need to enter a Description.";
 echo "<br /><br /><br /><br /><a href='#' onclick='javascript:history.go(-1);'>Go Back</a>";

 } else {
     // validation passes, so do something completely different

 $id2=mysql_insert_id();
 $year={$_POST['y']};
 $yr=substr($year,-2);
 $mth=$_POST['m'];
 $init={$_POST['initial']};	 
 $jobnumber=$yr.$mth.'-'.$id2.$init;

 $sql = INSERT INTO jobs VALUES (
 NULL, 
 '$yr',
 '$init',
 '{$_POST['y']-$_POST['m']-$_POST['d']}',
 '{$_POST['contact']}',
 '{$_POST['contactphone']}',
 '{$_POST['customer']}',
 '{$_POST['address']}',
 '{$_POST['city']}',
 '{$_POST['postal']}',
 '{$_POST['province']}',
 '{$_POST['description']}'
 NULL, 
 NULL, 
 '1', 
 CURRENT_TIMESTAMP
 )";
 mysql_query($sql) or die('Error, adding new job failed.  Check you fields and try again.');

 echo "You have successfully entered a new job.  The job number is $jobnumber and can be viewed <a href='http://here.com/'>here</a>";
 }


include 'close.php';
?>

 

also, how do i turn on error reporting?  might help me.

Link to comment
Share on other sites

No, not really. Close, but no cigar, as they say. This line is incorrect. Each element needs to be in braces, not the whole line . . .

 

'{$_POST['y']-$_POST['m']-$_POST['d']}',

 

And you really, really, really need to sanitize all form data before allowing it into a database query string.

 

Link to comment
Share on other sites

i tried how you said and still no cigar.

 

else {
     // validation passes, so do something completely different

 $id2=mysql_insert_id();
 $year=$_POST['y'];
 $yr=substr($year,-2);
 $mth=$_POST['m'];
 $init=$_POST['initial'];	 
 $jobnumber=$yr.$mth.'-'.$id2.$init;

 $sql = "INSERT INTO jobs VALUES (
 NULL, 
 '$yr',
 '{$init}',
 '{$_POST['y']}-{$_POST['m']}-{$_POST['d']}',
 '{$_POST['contact']}',
 '{$_POST['contactphone']}',
 '{$_POST['customer']}',
 '{$_POST['address']}',
 '{$_POST['city']}',
 '{$_POST['postal']}',
 '{$_POST['province']}',
 '{$_POST['description']}'
 NULL, 
 NULL, 
 '1', 
 CURRENT_TIMESTAMP
 )";
 mysql_query($sql) or die('Error, adding new job failed.  Check you fields and try again.');

 echo "You have successfully entered a new job.  The job number is $jobnumber and can be viewed <a href='http://here.com/'>here</a>";
 }

Link to comment
Share on other sites

Are you developing and debugging your code on a system with error_reporting set to E_ALL and display_errors set to on so that all the errors that php detects will be reported and displayed? You will save a ton of time.

 

Also, are you using mysql_insert_id() AFTER you have executed an INSERT query? That's the only time it holds a valid value.

Link to comment
Share on other sites

ok, so i changed it to :

 

else {
     // validation passes, so do something completely different

 $year=$_POST['y'];
 $yr=substr($year,-2);

 $sql = "INSERT INTO jobs VALUES (
 NULL, 
 '$yr',
 '{$_POST['initial']}',
 '{$_POST['y']}-{$_POST['m']}-{$_POST['d']}',
 '{$_POST['contact']}',
 '{$_POST['contactphone']}',
 '{$_POST['customer']}',
 '{$_POST['address']}',
 '{$_POST['city']}',
 '{$_POST['postal']}',
 '{$_POST['province']}',
 '{$_POST['description']}'
 NULL, 
 NULL, 
 '1', 
 CURRENT_TIMESTAMP
 )";
 mysql_query($sql) or die('Error, adding new job failed.  Check you fields and try again.');

 $mth=$_POST['m'];
 $init=$_POST['initial'];	
 $id2=mysql_insert_id();
 $jobnumber=$yr.$mth.'-'.$id2.$init;

 echo "You have successfully entered a new job.  The job number is $jobnumber and can be viewed <a href='http://here.com/'>here</a>";
 }

 

i have tried to turn on error reporting, i only get this though:

 

'Error, adding new job failed.  Check you fields and try again'

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.