...

View Full Version : ??Passing Form variables to mysql table



kesikkafa
12-31-2005, 01:02 PM
i have a table like this

http://www.kesikkafa.com/proj/1.gif

i want to enter info to this table via using this form.

http://www.kesikkafa.com/proj/2.gif

the code of my form is


<?php
$con=mysql_connect("localhost","root") or die("mysql error");
mysql_select_db("proje",$con) or die("database connection error");
$qry="select * from courses";
$result=mysql_query($qry,$con);

echo "<form action='reg.php' method='post'>";

echo "<table border=1><tr><td colspan='1'>";

echo "Student No:";
echo "<input type='text' name='stnr'>";
echo "<tr><td align='center'>";
while($row=mysql_fetch_array($result,MYSQL_ASSOC))
{
$r=$row['CourseName'];
$c=$row['CourseCode'];
echo $r;
echo "<input type='checkbox' name=$c']>";
echo "<br>";
}
echo"<tr><td align='right'>";
echo "<input type='submit' align='right'>";

echo "</table>";
echo "</form>";

?>

i need a reg.php to save this forms records to registered courses table.can you please help me

vinyl-junkie
12-31-2005, 05:41 PM
Let me see if I have this straight. You've written one script in this process, now you want us to write the rest of it for you? Sorry, but it looks like you're doing pretty good on your own. Why not give it a go yourself? Then, if it doesn't work for some reason, come back here with specific questions.

BTW, in the script you posted, I did note one small error here:


echo "<input type='checkbox' name=$c']>";

You need a left single quote around $c, like so:


echo "<input type='checkbox' name='$c']>";

kesikkafa
12-31-2005, 06:47 PM
vinyl-junkie

thank you ,

btw ,what i need is just an idea..i do pretty good yes you are right but at this point i am confused.if you dont want to help DON'T HELP

anyways,so should i use a loop in reg.php for pass this form records to mysql table??

vinyl-junkie
12-31-2005, 07:09 PM
if you dont want to help DON'T HELP

You may have misunderstood where I am coming from. For that matter, I may have misunderstood what kind of help you were looking for. Please accept my apologies if that is the case.

As I said in a post just yesterday to someone else, we are here to help you so that you can do the work yourself. We are NOT here to do the work for you, at least not in this forum. The Paid work offers and requests forum (http://www.codingforums.com/forumdisplay.php?f=36) is where you would go for that. :)


anyways,so should i use a loop in reg.php for pass this form records to mysql table??

Yes, that's exactly what you would do. You would have a while loop in reg.php, similar to the code you posted, except it would have something similar to this:


while($row=mysql_fetch_array($result,MYSQL_ASSOC))
{
$r=$row['CourseChecked'];
if ($r > "") {
// code here to insert data into database
}
}

kesikkafa
01-01-2006, 12:01 PM
thank you ,i will try

kesikkafa
01-01-2006, 02:14 PM
you wrote


// code here to insert data into database

before while loop i make and run a query like this


$qry="insert into registeredcourses(StudentNo,CourseCode) values($stn,'$a') ";
$result=mysql_query($qry,$con);

then should i re-write the query in the loop again?

i am asking stupid qustions i know ,i am confused

and what is CourseChecked

after check the courses and submit them via form ,i have variables like

$stn=$_POST['strn'];
$a=$_POST['c1'];
$b=$_POST['c2'];
$c=$_POST['c3'];
$d=$_POST['c4'];

in reg.php..

vinyl-junkie
01-02-2006, 01:00 AM
Sorry, I should have used the same variables you were using. Basically, what you want to do is make your checkbox an array, then process your array via a loop to insert records into your database. I'm getting *way* out of my area of expertise on this and may be leading you astray with exactly how this would work, but I think what you first need to do is change this statement in your code:


echo "<input type='checkbox' name=$c']>";

to this:


echo "<input type='checkbox' name=$c[]'>";

I found another forum post here (http://www.codingforums.com/showthread.php?t=64632&highlight=checkbox+array) which has some code similar to how I think this would work. Perhaps you can find some better examples by searching the forum. Or maybe someone else can provide some better code examples for you.

kesikkafa
01-02-2006, 09:20 AM
thank you ,i found the right code at another web site..i changed the checkbox to an array and use a foreach loop...


<?php
$con=mysql_connect("localhost","root") or die("mysql error");
mysql_select_db("proje",$con) or die("database connection error");


$stnr = $_POST['stnr'];
$courses = $_POST['courses'];

foreach($courses as $course)
{
$sql = "INSERT INTO registeredcourses (StudentNo, CourseCode) VALUES ($stnr, '$course')";
mysql_query($sql);

}

$af=mysql_affected_rows($con);

if($af>0)
{
echo "Courses Registered Succesfully";
}
else
{
echo "error";
}


?>

Velox Letum
01-02-2006, 07:37 PM
Alternatively, a single query which inserts multiple records...
(also changed error check to see if all rows were inserted correctly)


<?php
$con=mysql_connect("localhost","root") or die("mysql error");
mysql_select_db("proje",$con) or die("database connection error");


$stnr = $_POST['stnr'];
$courses = $_POST['courses'];

$sql = 'INSERT INTO registeredcourses (StudentNo, CourseCode) VALUES ';
foreach($courses as $course)
{
$sql_values[] = '("' . mysql_real_escape_string($stnr) . '","' . mysql_real_escape_string($course) . '")';
}
mysql_query($sql . implode(',', $sql_values));

$af=mysql_affected_rows($con);

if($af == count($courses))
{
echo "Courses Registered Succesfully";
}
else
{
echo "error";
}


?>

kesikkafa
01-02-2006, 09:44 PM
thank you ,can you please tell me about the disadvantages of this code




if($af>0)
{
echo "Courses Registered Succesfully";
}
else
{
echo "error";
}

Element
01-02-2006, 09:51 PM
thank you ,can you please tell me about the disadvantages of this code




if($af>0)
{
echo "Courses Registered Succesfully";
}
else
{
echo "error";
}

Because your not checking to see if ALL the courses were added. Your just checking if one or more are added. Velox usage of the count() function allows you to check if the number of courses submitted, were inserted into MySQL.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum