Page: 1, 2  Next

how can i insert my php variable in mysql?.
ace04
Status: Curious
Joined: 31 Jan 2006
Posts: 9
Location: philippines
Reply Quote
can anybody help me about this?.
here is my code:

if($submit == clickHere)
{
require_once('applicant.php');

echo"<tr><td align='center'>
<h2>No.
</td>
<td>
<h2>Name
</td>
<td align=center>
<h2>Age
</td>
<td>
<h2>School
</td>
<td colspan='3'>
<h2>Year Level/Grade
</td></tr>";
for($i=1; $i<$noDepend+1; $i++)
{
echo "<tr><td align=center>
<h2>$i
</td>
<td>
<input type=text name=dName[$i] size=26 >
</td>
<td align=left>
<input type=text name=dage[$i] size=2>
</td>
<td>
<input type=text name=dSchool[$i] size=25 >
</td>
<td>
<input type=text name=dYrlevel[$i] size=7 >
</td></tr>";
}
echo"<tr><td>&nbsp;</td></tr>";
}
?>

how can i insert those value in my textboxes?...
Back to top
jeffd
Status: Assistant
Joined: 04 Oct 2003
Posts: 594
Reply Quote
Hi Ace, it's hard to say without seeing the relevant code.

But you're not using the correct syntax for php. I'm assuming this is php of course. An array in php has to start with $, it's a variable like any other variable.

Currently, you're echoing out just the raw name for the variable, not the actual value for the variable.

So you need to have it more like this:

:: Code ::
$full_form = '<input type="text" value="' . $somearray[$i] . '">.... and so on

It's better to not use variables in " " even though you can, it makes it easier to read the code, and debug it.
Back to top
ace04
Status: Curious
Joined: 31 Jan 2006
Posts: 9
Location: philippines
Reply Quote
hmm... what i mean sir is that how can i insert the value of my textboxes into mysql?.. and is the correct syntax that i can be used?.. is it possible to insert an array in mysql?...
Back to top
jeffd
Status: Assistant
Joined: 04 Oct 2003
Posts: 594
Reply Quote
That question has been covered in some depth in this thread. Also a bit more in this thread.

The first one talks quite a bt about some problems that come up with arrays and mysql, and some possible solutions.

There's lots of ways to work around that though, One very simply way is to simply concatenate all the text field values into one long string using a unique separator, say a | or a ~ for example. You have to search all your input values to protect agains anyone using those in their input of course, changing a pipe to a broken pipe is one popular solution.

Then you just throw it al into one mysql table field. Not the most elegant, but it's quick, it's easy, and it works. Of course, the real question is why not just create more fields? Why the need to throw it all in one? It's a lot easier to process this stuff if you can just pull out the stuff you need.

When you want to get the data, you grab it from mysql, use explode to split the string into an array, then you can use that array on your page, if that's really how you want to do it.

personally, I wouldn't do that way, I'd create a unique user identifier, then create fields for each text box, then just fill in a row with each submission. It's the same thing, except you create one field per textbox instead of one field for all of them in an array. It makes no difference in terms of processing. When it makes little or no difference, I chose the easiest way, that is most flexible in terms of future change and upgrading. It's easier to upgrade or change when you have discreet fields.

You can read up on using arrays, but when you post code samples, try to post working code samples, otherwise it's impossible to tell you what is wrong. That's the live version, that is. With real variable syntax. Seeing little errors like that makes it hard to know if you're doing a little mistake or a major one. There are issues with arrays and mysql though, it's not as cut and dry as you might want.
Back to top
thanx
ace04
Status: Curious
Joined: 31 Jan 2006
Posts: 9
Location: philippines
Reply Quote
thanx 4 ur concern sir, for example i have a code like this

<input type=text name=name[0] value="">
<input type=text name=name[1] value="">

how can i insert this to mysql?...

thanx and god bless..
Back to top
erikZ
Status: Contributor
Joined: 30 May 2004
Posts: 148
Reply Quote
Hi ace04, it sounds like you don't have much idea about how forms, php, and mysql work together. Is this correct?

I'll give you the basic idea, but I won't write your programming for you. It's also not clear to me what those values you are using are, the things that look like array items but which aren't.

Anyway, I'll pretend that those are regular values:

  1. Step 1, the user fills out the form and submits it
  2. Step 2, the script takes the form data, probably through the $_POST variable set for each form value, and starts to work with it once it's received the request to do so from the first form page.
  3. Once the text box data is there, it has to be validated, and prepared for mysql insertion. If you have no idea of how to do basic mysql with php you'll have to start reading up on the question.
  4. The data is echoed on the page, if you want that to happen.

And that's how it works, more or less. Since it appears that you aren't sure how to do any of these steps, I'd recommend you start with learning how to use the $_POST data once the form is submitted, then learn about data validation and cleaning for mysql insertion, and then learn about how to construct a mysql query with PHP.

Good luck, just read on these topics and if you have more problems feel free to ask, but you have to show existing code samples from your project, and we can help you with the steps, but we don't write your code for you.
Back to top
how to use implode and explode?..
ace04
Status: Curious
Joined: 31 Jan 2006
Posts: 9
Location: philippines
Reply Quote
can anybody help me about implode and explode..
i have inserted my data using implode and i have made use of
"-" as a sign... how can i view this???....
Back to top
erikZ
Status: Contributor
Joined: 30 May 2004
Posts: 148
Reply Quote
For any explanation of PHP functions, always go first to php.net.

implode
explode
Back to top
is it possible to save from another page?..
ace04
Status: Curious
Joined: 31 Jan 2006
Posts: 9
Location: philippines
Reply Quote
can anybody help me about this?..
i have a page named "page1"
then i've inserted data's
and i also have "page2" and the submit button is located here
is it possible to have insertion from other page?..
Back to top
ace04
Status: Curious
Joined: 31 Jan 2006
Posts: 9
Location: philippines
Reply Quote
i'm having prblm executing this one:

for ($i="1"; $i<$dependent; $i++)
{
if(!checkifchar($dname[$i]))
{
$dname[$i]="";
}
unset($nb_dname);
$dname = implode("|", $dname);
$dage = implode("|", $dage);
$dschool = implode("|", $dschool);
$dyrlevel = implode("|", $dyrlevel);

$qdependent = "INSERT INTO dependent ('dependentID','dname','dage','dschool','dyrlevel' )
VALUES ('$i','$dname','$dage','$dschool','$dyrlevel')";
dbQuery($qdependent);

when i first use this.. it actually saves into mysql.. but now when im going to execute it again.. it can't insert into mysql anymore.. can you please give me an explanations or on how to solve this problem?.. thanx and godbless....
Back to top
Display posts from previous:   
Page: 1, 2  Next
All times are GMT - 8 Hours