简体   繁体   中英

php:process the select tag

this code run but when test it .. the data not correct i select data from database and i make it in array ad show it id by select tag but when select any id and submit .. example i select 5 and click on submit the record will delete is 2 not 5

<?php
require_once "config.php";
$qid="select id from info_user";
$arrayid=array();
$result=mysql_query($qid);
while($res=mysql_fetch_array($result)){
$arrayid[]=$res['id'];
}
var_dump($arrayid);
if(isset($_POST['sub'])){
$id=$_POST['id'];
$q="delete from info_user where id=$id ";
$qq=mysql_query($q);
if($qq){
    echo "you delete record ";
}
else{
    echo "error in deleting";
}}
?>
<html>
<head>
<title>delete</title>
</head>
<form action="delete.php" method="post">
<select name="id"><?php for($i=0;$i<count($arrayid);$i++){?>
<option value="<?php echo $i;?>"><?php echo $arrayid[$i];} ?></option></select> <br />
<input type="submit" name="sub" />     
</form>
</html>

I think that problem is in value for the options.

Try changing option line to

<option value="<?php echo $arrayid[$i];?>"><?php echo $arrayid[$i];} ?></option></select> <br />

Your <option> markup is wrong. Make your markup like this;

<select name="id">
  <?php for ($i = 0; $i < count($arrayid); $i++) { ?>
     <option value="<?php echo $i; ?>"><?php echo $arrayid[$i]; ?></option>
  } ?>
</select>

Tip: You'll find debugging easier if you indent your code. IDE's generally have this option. NetBeans is my favourite.

Instead of sending the ids, you're sending an array key associated to the id.

For instance, assume that: $arrayid = array(10, 11, 12); .

This is equivalent to: $arrayid = array(0 => 10, 1 => 11, 2 => 12); .

You'll see the options 10, 11 and 12, but you'll send either 0, 11 or 12, because that's what you're setting to the options values:

<select name="id">
   <option value="0">10</option>
   <option value="1">11</option>
   <option value="2">12</option>
</select>

If you select the option "11", the SQL statement to delete an entry will be:

delete from info_user where id=1

I did not test this code and I'm assuming the ids are integers, but try it:

<?php
require_once "config.php";
$qid     = "select id from info_user";
$arrayid = array();
$result  = mysql_query($qid);
while( $res = mysql_fetch_array($result) ){
    $arrayid[] = $res['id'];
}

if( isset($_POST['sub']) ){
    $id = (int)$_POST['id']; // make sure it's an integer to avoid SQL injections
    $q  = "delete from info_user where id=$id";
    $qq = mysql_query($q);
    if( $qq ) {
        $error = "you delete record ";
    } else {
        $error = "error in deleting";
    }
}
?>
<!DOCTYPE html>
<html>
    <head>
        <title>delete</title>
    </head>
    <body>
        <?php
        if( isset($error) ) :
            echo '<p>', htmlspecialchars($error), '</p>';
        elseif( !empty($arrayid) ) :
            var_dump($arrayid);
        endif;
        ?>

        <form action="delete.php" method="post">
            <select name="id">
                <?php foreach($arrayid as $id): ?>
                <option value="<?php echo (int)$id;?>">
                    <?php echo (int)$id; ?>
                <?php endforeach; ?>
                </option>
            </select>
            <br />
            <input type="submit" name="sub" />     
        </form>
    </body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM