Jump to content

Recommended Posts

heres my code not sure why it doesnt work...

 

<form action="insert.php" method="post">
name: <?php
$con = mysql_connect("xxx","xxx","xxx");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("xxx", $con);
$query="SELECT name FROM band_data2";

/* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */

$result = mysql_query ($query);
echo "<select name='name'> Name</option>";
// printing the list box select command

while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[id]>$nt[name]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box
?>
<br /> 
skill : <select name="skill"> 
<option value="">Do they hold their instuments up the right way?</option> 
<option value="1">1</option> 
<option value="2">2</option>
<option value="3">3</option> 
<option value="4">4</option>
<option value="5">5</option> 
<option value="6">6</option>
<option value="7">7</option> 
<option value="8">8</option>
<option value="9">9</option> 
<option value="10">10</option></select>

<input type="hidden" name="counter" value="1">
<input type="submit" />
</form>

<form action="insert.php" method="post">
name: <?php
$con = mysql_connect("xxx","xxx","xxx");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("xxx", $con);
$query="SELECT name FROM band_data2";

/* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */

$result = mysql_query ($query);
echo '<select name="name"> Name</option>';
// printing the list box select command

while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=\"{$nt['id']}\">{$nt['name']}</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box
?>
<br /> 
skill : <select name="skill"> 
<option value="">Do they hold their instuments up the right way?</option> 
<option value="1">1</option> 
<option value="2">2</option>
<option value="3">3</option> 
<option value="4">4</option>
<option value="5">5</option> 
<option value="6">6</option>
<option value="7">7</option> 
<option value="8">8</option>
<option value="9">9</option> 
<option value="10">10</option></select>

<input type="hidden" name="counter" value="1">
<input type="submit" />
</form>

 

Sorry, forum killed it before. I've tidied it up a bit, try wrapping variables contained in string with braces. I'm assuming you're running PHP5?

Since you didn't tell us what was not working, we're guessing here.

 

You are using the "id" from the database records, but you're not selecting it with your query.

 

<?php
mysql_select_db("xxx", $con);
$query="SELECT name, id FROM band_data2";
$result = mysql_query ($query);
echo '<select name="name"> Name</option>';
// printing the list box select command

while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
     echo "<option value='{$nt['id']}'>{$nt['name']}</option>\n";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box
?>

 

Ken

Thanks but even with your revisions its still not doing what I want. To be honest Ive only been learning for a week. I can follow most of what the script is doing but havent got a clue how to edit to do what I want.

What I want is a drop down list populated with my "name" field from my database, once the name is selected I want to pass the "name" data on to insert.php as $_POST data. I did have a text box that was working but in order to update the table you have to, obviously, type the name exactly.

Ok. Please post you current code for both the form and insert.php

 

What is happening now and why is it different from what you want? Have you looked at the generated source to make sure that the generated HTML is correct?

 

Ken

It also wouldn't hurt to remove the 'Name' string from within the <select></select> tag's structure . . .

 

echo 'Name:<select name="name">';
// instead of //
echo "<select name='name'> Name</option>";

Heres vote.php. The code is taken from a tutorial on the web,

<form action="insert.php" method="post">
name: <?php
$con = mysql_connect("xxx","xxx","xxx");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("a3362005_bands", $con);
$query="SELECT name FROM band_data2";

/* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */

$result = mysql_query ($query);
echo 'Name:<select name="name">';
// printing the list box select command

while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=\"{$nt['id']}\">{$nt['name']}</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box
?>
<br /> 
skill : <select name="skill">
<option value="1">1</option> 
<option value="2">2</option>
<option value="3">3</option> 
<option value="4">4</option>
<option value="5">5</option> 
<option value="6">6</option>
<option value="7">7</option> 
<option value="8">8</option>
<option value="9">9</option> 
<option value="10">10</option></select>
<input type="hidden" name="counter" value="1">
<input type="submit" />
</form></center>

 

Heres the insert.php

<?php
$con = mysql_connect("xxx","xxx","xxx");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("a3362005_bands", $con);
//Add to the table value with form data.
mysql_query("UPDATE band_data2 SET skill = (skill + {$_POST['skill']}) WHERE name='{$_POST['name']}'");
mysql_query("UPDATE band_data2 SET skill_ave = (skill / counter) WHERE name='{$_POST['name']}'");
echo 'vote counted';
mysql_close($con);
print_r ( $_POST );

?>

 

And here is the output from  "print_r ( $_POST );"  "Array ( [name] => [skill] => 10 [counter] => 1 )" i was told to put this in so i could see what was going on. As you can see the "name" from the dropdown isnt making it to the insert.php.

Since you didn't tell us what was not working, we're guessing here.

 

You are using the "id" from the database records, but you're not selecting it with your query.

 

<?php
mysql_select_db("xxx", $con);
$query="SELECT name, id FROM band_data2";
$result = mysql_query ($query);
echo '<select name="name"> Name</option>';
// printing the list box select command

while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
     echo "<option value='{$nt['id']}'>{$nt['name']}</option>\n";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box
?>

 

Ken

 

 

He has  a point, I can't see this amendment in your revised code, perhaps try this? just after the mysql query try

 

echo mysql_error();

 

And see if there's an error preventing the values being returned to the select field if it has no output, chances are it has no value.

Fixed it sort of. Someone else on another forum had exactly the same problem so copied there revised code. Its still not working properly. It will only return the first word in the "name" field.

 

As above but:

while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[name]>name: $nt[name]}</option>";

 

Now print_r ( $_POST ); gives: Array ( [name] => Coheed [skill] => 10 [counter] => 1 ) I want it to read: ( [name] => Coheed and Cambria [skill] => 10 [counter] => 1 )

Since you aren't using the "id" field in your query, change your code for vote.php to

<form action="insert.php" method="post">
<?php
$con = mysql_connect("xxx","xxx","xxx");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("a3362005_bands", $con);
$query="SELECT name FROM band_data2";

/* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */

$result = mysql_query ($query);
echo 'Name:<select name="name">';
// printing the list box select command

while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=\"{$nt['name']}\">{$nt['name']}</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box
?>
<br /> 
skill : <select name="skill">
<option value="1">1</option> 
<option value="2">2</option>
<option value="3">3</option> 
<option value="4">4</option>
<option value="5">5</option> 
<option value="6">6</option>
<option value="7">7</option> 
<option value="8">8</option>
<option value="9">9</option> 
<option value="10">10</option></select>
<input type="hidden" name="counter" value="1">
<input type="submit" />
</form></center>

 

Also change the code for insert.php to

<?php
echo '<pre>' . print_r($_POST,true) . '</pre>';
$con = mysql_connect("xxx","xxx","xxx") or die("Could not connect<br>" . mysql_error());
mysql_select_db("a3362005_bands", $con);
//Add to the table value with form data.
$query = "UPDATE band_data2 SET skill = (skill + {$_POST['skill']}), skill_ave = (skill / counter) WHERE name='{$_POST['name']}'";
$rs = mysql_query($query) or die("Problem with the update query: $query<br>" . mysql_error());
echo 'vote counted';
?>

 

You only need one update query.

 

Note: the reason you're new code is only getting one name is that the value is not enclosed in quotes. If you had looked at the generated HTML this would have been apparent.

<?php
echo "<option value=$nt[name]>name: $nt[name]}</option>";
?>

 

See my version above.

 

Ken

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.