please help me...i need some help from you all....because i still new in writhing php code.

thanks...

actually right now im doing a vote list name member for the committee members. so, the name for the list Committee members i take from my database. After that, i want when the people click submit when the name already chosen ....the data must insert into database but in difference table.

here is my code.....

first page i rename it select.php

<?
session_start ();
include "connection.php";
mysql_connect ("$host", "$username", "$password") or die ("cannot connect");
mysql_select_db ("$db_name") or die ("cannot select DB");
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>

<body>
<form name="form1" method="post" action="save.php">
  <table width="35%" border="1" align="center">
    <tr>
      <td colspan="2">Who you want to vote as a ......</td>
    </tr>
    <tr>
      <td colspan="2">&nbsp;</td>
    </tr>
    <tr>
      <td width="9%" height="26">Chairman</td>
      <td>
  <label>
  <select name="select1" id="select" >
  <?
$sql = "select UserName from advlogin order by UserName ASC";
$rs = mysql_query($sql) or die (mysql_error());

if ($rs)
{
    while ($rw = mysql_fetch_object($rs))
    {
        $UserName=$rw->UserName;
?>
  <option value="<? echo $UserName; ?>"<?php if(!empty($row['option']) and ($row['option'] == "$UserName")) { echo(" selected=\"selected\""); }?>><? echo $UserName; ?> </option>
<?
}

}

?>
  </select> 
  </label>
      </td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><label>
        <input type="submit" name="button" value="Submit">
      </label></td>
    </tr>
  </table>
</form>
</body>
</html>

here is my save.php code...

<?
include "con3.php";

 mysql_connect("$host", "$username", "$password")or die("cannot connect");
 mysql_select_db("$db_name")or die("cannot select DB");

     $UserName = $_POST['UserName'];


 $sql="insert into vote(name)values('$name')";

mysql_query($sql) or die(mysql_error());

?>

<style type="text/css">
<!--
body {
    background-color: #D5EDB3;
}
-->
</style><HEAD>
<script type = "text/javascript">
window.location="#"
</script>

the problem is...when i select the name and click submitt..it works but..the name doesn't insert into my database.

...
please help me... :(

for a start change

$sql="insert into vote(name)values('$name')";

to this

$sql="insert into vote(name)values('$username')";

thanks for the quick response mathieu89

i'm appreciate it

ok...i have change it like this as you tell me

$sql="insert into vote(name)values('$username')";


but..when i check my database ... the name i have been chosen didn't insert. they insert "root" without quote. in my database name testing and my table name vote( only have id and name) for id i have set auto_increment.

Well in that case it could be that your $username variable is the same as the mysql connection variable $username. I guess its inserting your database username root???

maybe make your form variable something other than $username

mathieu89

i still can't insert the name i have been selected into my database. i don't know why my code doesn't want to work. :(:(:(:(

pleasseee help me...

First of all, use CODE to post your code. I can't tell which line u got errors.
As much as i can read, $row came out from nowhere in select.php (first is $rw). then in save.php, '$name' should be '$UserName'. I think php is case-sensitive.

if ($rs)
{
while ($rw = mysql_fetch_object($rs))
{
$UserName=$rw->UserName;
?>
<option value="<? echo $UserName; ?>"<?php if(!empty($row['option']) and ($row['option'] == "$UserName")) { echo(" selected=\"selected\""); }?>><? echo $UserName; ?> </option>
<?
}

}

?>

For this one u can use echo simply like this.

if ($rs)
{
while ($rw = mysql_fetch_object($rs))
{
$UserName=$rw->UserName;

 echo "<option value='$UserName'";
 if(!empty($rw['option']) and ($rw['option'] == "$UserName"))
 { 
 echo "selected='selected'";
 }
echo ">";
echo "$UserName;</option>";
}

}

I'm not saying that u'r wrong but too complex and hard to read. (mean hidding place for errors if any)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.