I need help to get records from a table and view as radio button and insert one of this records depend for what the user choose to another table and i view my records in radio buttons but when i try to select a radio button to insert to another table nothing happened.
<form method="post" action="">
<?php
$link=mysql_connect("localhost","root","")or die("Can't Connect...");
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER 'utf8'");
mysql_select_db("project",$link) or die("Can't Connect to Database...");
$query=mysql_query("SELECT * FROM mrsh7on WHERE `major`='$major'");
if (mysql_num_rows($query) > 0)
{
while($row = mysql_fetch_assoc($query))
{
echo"<div>";
echo "<input name=\"radio\" type=\"radio\" required value=".$row['vip_id'].">
".$row['name']."<br />";
echo"</div>";
}
$name = $row['name'];
$vip_id = $row['vip_id'];
}
if(isset($_POST['submit']) and !empty($_POST['submit']))
{
if(!empty($_POST['radio']))
{
$id = $_SESSION['sess_id'];
$major=$_SESSION['sess_major'];
$vip_id=$_SESSION['sess_vip_id'];
$name=$_SESSION['sess_name'];
$link=mysql_connect("localhost","root","")or die("Can't Connect...");
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER 'utf8'");
mysql_select_db("project",$link) or die("Can't Connect to Database...");
$query=mysql_query("SELECT * FROM vote WHERE `id`='$id'");
if (mysql_num_rows($query) > 0)
{
echo"this user voted before";
}
else
{
$sql = mysql_query("INSERT INTO vote (id, major, vip_id, name)
VALUES ('$id', '$major', '$vip_id', '$name')");
mysql_query($sql,$link) or die("mysql_error()");
echo"Successfully Inserted !";
}
}
}?>
<input type="button" name="submit" value="submit">
</form>
1) If that is all of your code. Starting off from a million dollar question, Where have you started sessions? Missing session_start();
in the beginning of your code.
2) "SELECT * FROM mrsh7on WHERE
major='$major'"
Where are you getting the $major
from? try echo $major;
and see what you get.
3) mysql_query($sql,$link) or die("mysql_error()");
This is not the correct way, rather:
mysql_query($sql) or die(mysql_error());
4) Change this: <input type="button" name="submit" value="submit">
to this:
<input type="submit" name="submit" value="submit">
Note: If for all that works out, Please avoid using the deprecated mysql functions and move to either
mysqli
orPDO
.