Cannot insert data to database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lala
Forum Commoner
Posts: 27
Joined: Mon Jul 26, 2010 7:56 pm

Cannot insert data to database

Post by lala »

I faced problem in inserting data to database.
When i try to insert software:
$temp = $_POST["sw$i"];
$temp_lk = $_POST["skey$i"];
But it's unable to save the data
i do not know whether i miss pass any parameter for this code.
Can anyone help me, Thanks.

This is part of the code

Code: Select all


   $temp = $_POST["sw$i"];
   $temp_lk = $_POST["skey$i"];


Last edited by lala on Tue Sep 14, 2010 9:15 pm, edited 1 time in total.
TonsOfFun
Forum Commoner
Posts: 54
Joined: Wed Jun 02, 2010 7:37 pm

Re: Cannot insert data to database

Post by TonsOfFun »

I have never done but when you add a variable to text, it should be appended.
So try changing:

Code: Select all

$temp = $_POST["sw$i"];
$temp_lk = $_POST["skey$i"];
To:

Code: Select all

$temp = $_POST['sw' . $i];
$temp_lk = $_POST['skey' . $i];
User avatar
angelicodin
Forum Commoner
Posts: 81
Joined: Fri Nov 13, 2009 3:17 am
Location: Oregon, USA

Re: Cannot insert data to database

Post by angelicodin »

The way TonsOfFun put might work, but I would just concatenate it outside of the of post.

EDIT: I meant that TonsOfFun is absolutely correct that you need to concatenate the variable set. And I am not sure mine will work, I guess the question would be is the sw and skey post calls defined in addition with $i.

Code: Select all

$temp = $_POST['sw' ] . $i;
$temp_lk = $_POST['skey'] . $i;
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Cannot insert data to database

Post by califdon »

So you expect your form field data to include such element names as sw1, sw2, sw3, ... skey1, skey2, etc. etc.? I think your original code is correct for that. You say it's unable to save the data...what does that mean? Do you get a MySQL error? What does it say? Following the line where you assign the value to $sql, try echoing the value of $sql so that you can see what is or isn't there.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Cannot insert data to database

Post by AbraCadaver »

If that doesn't work, try:

Code: Select all

$sql="insert FORCEFULLY into v_swlk (v_sw, v_sw_lk, v_equip_id) values ('$temp', '$temp_lk', '$id')";
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
lala
Forum Commoner
Posts: 27
Joined: Mon Jul 26, 2010 7:56 pm

Re: Cannot insert data to database

Post by lala »

Thanks for all the replies.
However, it doesn't work too.

I just wonder whether is other pass value problem or what because this page contain if else loop.
The code i post is from if part while else loop can work perfectly with:
$temp = $_POST["sw$i"];
$temp_lk = $_POST["skey$i"];
I still can't figure out the problem thus far. :crazy:
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Cannot insert data to database

Post by califdon »

Have you echoed out the SQL statement as I suggested? Does it look correct? There is no way to answer your questions if you don't try to help yourself.
lala
Forum Commoner
Posts: 27
Joined: Mon Jul 26, 2010 7:56 pm

Re: Cannot insert data to database

Post by lala »

califdon wrote:Have you echoed out the SQL statement as I suggested? Does it look correct? There is no way to answer your questions if you don't try to help yourself.
Hello califdon,

I have echoed some of it at the end of the script
echo $_POST["sw$i"];// it show the right value
echo $_POST["sw2"];// it can show the 2nd sw that i added
echo $_POST["skey$i"];// can show too
while i echoed $temp, $temp_lk, i doesnt show

then if i echoed inside the for loop, it cant show any value.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Cannot insert data to database

Post by califdon »

OK, then apply some logic to what you found:
  • the correct values are coming from the form and are in the $_POST array
  • these values are not getting into the variables $temp and $temp_lk
  • therefore, the problem is at the point where you assign values to those variables
Which brings us to the expression $_POST["sw$i"]. That looked to me like it should work, but the above logic makes it pretty clear that it isn't working. So what can you do? Well, first you could check to see that there is actually a value in $i at that point. Try echoing out the value of $i just before the assignments to those variables. Try echoing out the expression sw$i and see what value that produces. Maybe you will see something wrong there. If not, I could be mistaken about the syntax $_POST["sw$i"], although I thought that I had used something like that once or twice. The important thing is for you to learn how to search for a problem like this so the next time you have some kind of a PHP problem, you will be better equipped to handle it. Let us know what you find out.
lala
Forum Commoner
Posts: 27
Joined: Mon Jul 26, 2010 7:56 pm

Re: Cannot insert data to database

Post by lala »

califdon wrote:OK, then apply some logic to what you found:
  • the correct values are coming from the form and are in the $_POST array
  • these values are not getting into the variables $temp and $temp_lk
  • therefore, the problem is at the point where you assign values to those variables
Which brings us to the expression $_POST["sw$i"]. That looked to me like it should work, but the above logic makes it pretty clear that it isn't working. So what can you do? Well, first you could check to see that there is actually a value in $i at that point. Try echoing out the value of $i just before the assignments to those variables. Try echoing out the expression sw$i and see what value that produces. Maybe you will see something wrong there. If not, I could be mistaken about the syntax $_POST["sw$i"], although I thought that I had used something like that once or twice. The important thing is for you to learn how to search for a problem like this so the next time you have some kind of a PHP problem, you will be better equipped to handle it. Let us know what you find out.
Thanks for your advice. Now i had found the error in pass num from other page which is my mistake. There are still have 2 PHP Script that I had to check the error.
and now for sure is there is no syntax for $_POST["sw$i"] :wink:
lala
Forum Commoner
Posts: 27
Joined: Mon Jul 26, 2010 7:56 pm

Re: Cannot insert data to database

Post by lala »

I had faced another problem.
I want insert 2 value into database using same method.
Value A cannot insert while value B can be inserted.

When I try to echoed at the end of the page, ValueA shows its value while ValueB does not show.
This situation is different as before :?
Post Reply