Page 1 of 1
Cannot insert data to database
Posted: Mon Sep 13, 2010 12:43 am
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"];
Re: Cannot insert data to database
Posted: Mon Sep 13, 2010 9:24 am
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];
Re: Cannot insert data to database
Posted: Mon Sep 13, 2010 3:28 pm
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;
Re: Cannot insert data to database
Posted: Mon Sep 13, 2010 4:18 pm
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.
Re: Cannot insert data to database
Posted: Mon Sep 13, 2010 4:38 pm
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')";
Re: Cannot insert data to database
Posted: Mon Sep 13, 2010 8:42 pm
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.

Re: Cannot insert data to database
Posted: Mon Sep 13, 2010 9:02 pm
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.
Re: Cannot insert data to database
Posted: Tue Sep 14, 2010 2:22 am
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.
Re: Cannot insert data to database
Posted: Tue Sep 14, 2010 12:24 pm
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.
Re: Cannot insert data to database
Posted: Tue Sep 14, 2010 9:18 pm
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"]

Re: Cannot insert data to database
Posted: Wed Sep 15, 2010 1:51 am
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
