/    Sign up×
Community /Pin to ProfileBookmark

Select and insert query question (PHP/MySQL)

Please help.

I am trying to select some information from the first table and insert information selected from the first table into the second table row by row. My code is shown below:

//////////////////////////////////////////////////////////////////////

if (isset($Test))
{
include (“dbConnection.php”);

$sql = “select * from check where paymentAmount=’18′”;

if (! $result = mysql_query($sql))
{
die(“bad query”);
}

while ($row = mysql_fetch_assoc($result))
{
$championID = $row[“championID”];
$paymentAmount = $row[“paymentAmount”];
$dateSent = $row[“dateSent”];
$dateReceived = $row[“dateReceived”];

$sql = “insert into mailing_check values (‘$championID’, ‘$paymentAmount’, ‘$dateSent’, ‘$dateReceived’)”;

if (! $result = mysql_query($sql))
{
die(“bad query.”);
}

} // End while

} // End if (isset($Test))

////////////////////////////////////////////////////////////////

I know that there should be 4 rows of record selected from the check table like this:

0010, 18.00, 2004-06-17, 2004-06-19
0017, 18.00, 2004-06-23, 2004-06-24
0030, 18.00, 2004-07-12, 2004-07-14
0036, 18.00, 2004-07-13, 2004-07-15

However, it only inserts the first row:

“0010, 18.00, 2004-06-17, 2004-06-19”

into mailing_check table instead of all 4 rows.

I don’t know why it inserts only the first row but not all 4 rows. Anyone know what I did wrong in my code?

Thanks very much

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@solavarJul 15.2004 — See if naming the fields will make a difference

In other words:

You have...

[code=php]

$sql = "insert into mailing_check values ('$championID', '$paymentAmount', '$dateSent', '$dateReceived')";

[/code]


...try

[code=php]

$sql = "insert into mailing_check (championID, paymentAmount, dateSent, dateReceived) values ('$championID', '$paymentAmount', '$dateSent', '$dateReceived')";

[/code]
Copy linkTweet thisAlerts:
@Paul_JrJul 16.2004 — [font=palatino linotype]It could be as a result from the first query being incorrect. Integers should not be quoted. You have the 18 in the first query quoted. Try removing the single quotes and see what happens.[/font]
Copy linkTweet thisAlerts:
@learner007authorJul 16.2004 — Thanks guys for replying.

I already tried both of your suggestions, and it still does not work. I really don't know why now.
Copy linkTweet thisAlerts:
@crh3675Jul 17.2004 — You are resetting the $result variable with your second query. Change the second query to :

<i>
</i>$sql = "insert into mailing_check values ('$championID', '$paymentAmount', '$dateSent', '$dateReceived')";

if (! $result2 = mysql_query($sql))
{
die("bad query.");
}
Copy linkTweet thisAlerts:
@Stephen_PhilbinJul 17.2004 — Hi guys. I'm just starting to get to grips with MySQL and PHP and I'm having some "fun" trying to get an insert query to behave for me. I just keep getting a MySQL syntax error for almost everything I try (if not that then a php sytax error).

I'll give you the code I've written so far and the error it gives me. I don't quite get what the error message says though as it seems to refer to some code that is not there:

[code=php]$inputquery = "INSERT INTO mhcms ('stryid','strytitle','strytext','imgpath') VALUES ('null','$first','$second','$third')";

mysql_query($inputquery) or die("Something went a bit wrong: " . mysql_error());[/code]


and here's the error notice it kicks out from the or die thing:

Something went a bit wrong: 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 ''stryid','strytitle','strytext','imgpath') VALUES ('null','',''[/QUOTE]

Notice the two single quotes right at the start of the error read out: ''stryid','

Even though in my actual code there is only one single quote there. Could this seemingly automatic insertion of an extra single quote be the cause of my woes?

There's two other bits of code ya might need to see to be able to tell me what my inevitably simple and glaringly obvious problem is. these are some variables:

[code=php]$Array['one'] = $first;
$Array['two'] = $second;
$Array['three'] = $third;[/code]


Which I decided to use because I started to get the impression that the [''] characters of the array might be causeing MySQL syntax errors.

The other code ya might wanna see is the form its self:

[code=php]<?php
echo "<form name="submitions" method="post" action="input.php" enctype="multipart/form-data">"
. "<p>Title: <input type="text" name="Array[one]"></p>"
. "<textarea name="Array[two]" cols="30" rows="6"></textarea>"
. "<p>Image address: <input type="text" name="Array[three]"></p>"
. "<input type="submit" value="Submit">"
. "</form>"
?>[/code]


I really hope you guys can help because I'm stuck bigtime and just can't seem to find the info I need in the php and MySQL manuals. ?
Copy linkTweet thisAlerts:
@ShrineDesignsJul 17.2004 — use back ticks ()<br/>
&quot;INSERT INTO </code></span></C>mhcms<C><span><code> (</code></span></C>stryid<C><span><code>, </code></span></C>strytitle<C><span><code>, </code></span></C>strytext<C><span><code>, </code></span></C>imgpath`) VALUES ('', '$first', '$second', '$third')"
Copy linkTweet thisAlerts:
@Stephen_PhilbinJul 17.2004 — Ah great stuff. I did use back ticks before but still got an error. It's the same error I'm getting now.

Say if I put "first" in my first text box, and "second" in my second text box and so on, I now get the error:

Something went a bit wrong: Unknown column 'first' in 'field list'[/QUOTE]

Would I be right in assuming that the error message is saying that it's looking for a field in my table called first (ie whatever is entered into the text box) to put data in to? If this is the case then why is it doing it? Or am I just misunderstanding what it means by column and field list?

Oh I also get a MySQL sytax error if I use more than one word in a text box or textarea.

The error message is:

Something went a bit wrong: 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 'field,second field,third field)' at line 1[/QUOTE]

Here's how my form and action code look now.

Form:[code=php]<?php
echo "<form name="submitions" method="post" action="input.php">"
. "<p>Title: <input type="text" name="Array[one]"></p>"
. "<textarea name="Array[two]" cols="30" rows="6"></textarea>"
. "<p>Image address: <input type="text" name="Array[three]"></p>"
. "<input type="submit" value="Submit">"
. "</form>"
?>[/code]


and my action code:
[code=php]
<?php
$inputquery = "INSERT INTO mhcms (title,strytext,imgpath) VALUES ('null','$Array[one]','$Array[two]','$Array[three]')";
mysql_query($inputquery) or die("Something went a bit wrong: " . mysql_error());
?>
[/code]


Many thanks in advance guys. ?
Copy linkTweet thisAlerts:
@solavarJul 17.2004 — Before you insert the stuff into the database, see what values you are trying to insert. Do they look reasonable?

[code=php]
/* These are the values to go into the DB

'$Array[one]','$Array[two]','$Array[three]'

What are they?
*/
$first = $Array[one];
$second = $Array[two];
$third = $Array[three];

// echo them and see what they contain
echo "First: $first <br />Second: $second<br />Third: $third";

// Do they make sense? If they do, use these variables in the SQL instead of $Array[one] etc...

[/code]
Copy linkTweet thisAlerts:
@solavarJul 17.2004 — We seem to have two topics here, do we?
Copy linkTweet thisAlerts:
@learner007authorJul 20.2004 — Thanks all for your comments and helps. Thank you, Craig. You're exactly right.
×

Success!

Help @learner007 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.22,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...