Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

The while loop checkbox name is checkextra[] and value is price echo using php <?php echo $show_extra['price']; ?>

Now the following code only workable for the price - it save perfectly with the checkbox i checked. But the $show_extra['item_name'] are not saving accordingly. In DB all the item_name are giving me the last while loop item , it not same like the price save perfectly.

The code bellow i have modify a lot of time but the output of item_name still same

<div class="adjust">
<center><form class="greatForm" method="post" action="#">
<?php
$read_extra = mysql_query("select * from extra_item where theme_name = '$title'");

while($show_extra = mysql_fetch_array($read_extra))
{
    $getextraname = $show_extra['item_name'];

?>
    <input type="checkbox" name="checkextra[]" id="<?php echo       $show_extra['item_label']; ?>" value="<?php echo $show_extra['price']; ?>"/>
    <label for="<?php echo $show_extra['item_label']; ?>" >
    <img style="height:150px;width:150px;" src="<?php echo 'themeinfo/extra/'.$show_extra['item_img']; ?>"/><?php echo $show_extra['item_name']; ?>  </label>

<?php
}
 ?>
<center><br><br><input type="submit" class="button" style="width:10%;" value="SAVE"  name="choose_extra"></center></br>
</form></center>
<?php


    if(isset($_POST['choose_extra']))
{
    if(!empty($_POST['checkextra'])) {
    foreach($_POST['checkextra'] as $getextraprice) {
    $takeextra = mysql_query("INSERT INTO selectextra(user,title,extraitem,price) 
                            VALUES('".mysql_real_escape_string($username)."',
                                '".mysql_real_escape_string($title)."',
                                '".mysql_real_escape_string($getextraname)."',
                                '".mysql_real_escape_string($getextraprice)."')") or die(mysql_error());

        }
        if($takeextra)
            {
                echo "<center>Extra item order SAVE.</center>";
            }
            else{
                echo "failed";
            }
    }
}

?>

</div>

DB item_name all get the same

-- Table structure for table `extra_item`
--

CREATE TABLE IF NOT EXISTS `extra_item` (
`theme_name` varchar(500) NOT NULL,
`item_name` varchar(500) NOT NULL,
`item_label` varchar(500) NOT NULL,
`price` int(10) NOT NULL,
`item_img` varchar(500) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--

-- Table structure for table `selectextra`
--

CREATE TABLE IF NOT EXISTS `selectextra` (
`user` varchar(500) NOT NULL,
`title` varchar(500) NOT NULL,
`extraitem` varchar(500) NOT NULL,
`price` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--

- - Dumping data for table selectextra --

INSERT INTO `selectextra` (`user`, `title`, `extraitem`, `price`) VALUES
('qweqwe', 'test', 'red balloon4', 10),
('qweqwe', 'test', 'red balloon4', 10),
('qweqwe', 'test', 'red balloon4', 1),
('qweqwe', 'test', 'red balloon4', 1),
('qweqwe', 'test', 'red balloon4', 1),
('qweqwe', 'test', 'red balloon4', 2),
share|improve this question
    
mysql_query is deprecated, do some research into mysqli_query and you should receive a more detailed error after implementing. – CoderDojo May 19 '14 at 11:56
    
I think that in your queries you need to take $title out of the single quotes because the value in that variable won't be picked up. So it should read: "select * from theme where title = '" . $title . "' – user1849060 May 19 '14 at 11:56
    
@CoderDojo but im using phpmyadmin – user3652484 May 19 '14 at 12:00
    
@user1849060 hi , but the problem is not there. And that one also not a checkbox and while loop, i asking for insert problem – user3652484 May 19 '14 at 12:00
    
Your insert is not in a loop. It could only do one insert and doesn't seem to take any notice of which checkboxes are checked. – Kickstart May 19 '14 at 12:22

Changed to mysqli, and with some messages to put out debugging info, try this (set up the database connection fields to suit your db):-

<div class="adjust">
<?php

$link = new mysqli("hostname", "username", "password", "databasename");

if ($link->connect_errno) 
{
    printf("Connect failed: %s\n", $link->connect_error);
    exit();
}

?>
<center><form class="greatForm" method="post" action="#">
<?php

$sql = "SELECT price, item_name, item_label, item_img FROM extra_item WHERE theme_name = '$title'";

if ( ($read_extra = $link->query($sql)===false )
{
  echo "Invalid query: ".$link->error."\r\n $sql\r\n";
  exit();
}

while($show_extra = $read_extra->fetch_array(MYSQLI_ASSOC))
{
    $_SESSION['getextraprice'] = $show_extra['price'];
    $_SESSION['getextraname'] = $show_extra['item_name'];
?>
    <input type="checkbox" name="<?php echo $show_extra['price']; ?>" id="<?php echo $show_extra['item_label']; ?>" value="<?php echo $show_extra['item_label']; ?>"/>
    <label for="<?php echo $show_extra['item_label']; ?>" >
    <img style="height:150px;width:150px;" src="<?php echo 'themeinfo/extra/'.$show_extra['item_img']; ?>"/><?php echo $show_extra['item_name']; ?></label>

<?php
}
 ?>
<center><br><br><input type="submit" class="button" style="width:10%;" value="SAVE" name="choose_extra"></center></br>
</form></center>
<?php
    if(isset($_POST['choose_extra']))
    {
        $getextraprice = $_SESSION['getextraprice'];
        $getextraname = $_SESSION['getextraname'];
        $sql = "INSERT INTO selectextra(user,title,extraitem,price) VALUES('$username','$title','$getextraname,'$getextraprice')";
        if ( ($insert = $link->query($sql)===false )
        {
          echo "failed: ".$link->error."\r\n $sql\r\n";
        }
        else
        {
            echo "Order SAVE.";
        }
    }
?>

</div>

</div>

EDIT - if you really want to use mysql_* calls then try this, which will at least let you know what the errors are:-

<div class="adjust">
<center><form class="greatForm" method="post" action="#">
<?php
$read_extra = mysql_query("SELECT price, item_name, item_label, item_img FROM extra_item where theme_name = '".mysql_real_escape_string($title)."'") or die(mysql_error());

while($show_extra = mysql_fetch_array($read_extra))
{
    $_SESSION['getextraprice'] = $show_extra['price'];
    $_SESSION['getextraname'] = $show_extra['item_name'];
?>
    <input type="checkbox" name="<?php echo $show_extra['price']; ?>" id="<?php echo $show_extra['item_label']; ?>" value="<?php echo $show_extra['item_label']; ?>"/>
    <label for="<?php echo $show_extra['item_label']; ?>" >
    <img style="height:150px;width:150px;" src="<?php echo 'themeinfo/extra/'.$show_extra['item_img']; ?>"/><?php echo $show_extra['item_name']; ?></label>

<?php
}
 ?>
<center><br><br><input type="submit" class="button" style="width:10%;" value="SAVE" name="choose_extra"></center></br>
</form></center>
<?php
    if(isset($_POST['choose_extra']))
    {
        $getextraprice = mysql_real_escape_string($_SESSION['getextraprice']);
        $getextraname = mysql_real_escape_string($_SESSION['getextraname']);
        $takeextra = mysql_query("INSERT INTO selectextra(user,title,extraitem,price) VALUES('".mysql_real_escape_string($username)."','".mysql_real_escape_string($title)."','$getextraname,'$getextraprice')") or die(mysql_error());

        if($takeextra)
        {
            echo "Order SAVE.";
        }
        else{
            echo "failed";
        }
    }
?>

</div>

</div>

If you want to get the checkboxes that have been ticked then something like this would do it:-

<div class="adjust">
<center><form class="greatForm" method="post" action="#">
<?php
$read_extra = mysql_query("SELECT price, item_name, item_label, item_img FROM extra_item where theme_name = '".mysql_real_escape_string($title)."'") or die(mysql_error());

while($show_extra = mysql_fetch_array($read_extra))
{
    $_SESSION['getextraprice'] = $show_extra['price'];
    $_SESSION['getextraname'] = $show_extra['item_name'];
?>
    <input type="checkbox" name="<?php echo $show_extra['price']; ?>" id="<?php echo $show_extra['item_label']; ?>" value="<?php echo $show_extra['item_label']; ?>"/>
    <label for="<?php echo $show_extra['item_label']; ?>" >
    <img style="height:150px;width:150px;" src="<?php echo 'themeinfo/extra/'.$show_extra['item_img']; ?>"/><?php echo $show_extra['item_name']; ?></label>

<?php
    if(isset($_POST['choose_extra']) and array_key_exists($show_extra['price'], $_POST) and $_POST[$show_extra['price']] != '')
    {
        $takeextra = mysql_query("INSERT INTO selectextra(user, title, extraitem, price) 
                                VALUES('".mysql_real_escape_string($username)."',
                                    '".mysql_real_escape_string($title)."',
                                    '".mysql_real_escape_string($show_extra['item_name'])."',
                                    '".mysql_real_escape_string($_POST[$show_extra['price']])."')") or die(mysql_error());
    }
}
 ?>
<center><br><br><input type="submit" class="button" style="width:10%;" value="SAVE" name="choose_extra"></center></br>
</form></center>
</div>
</div>

Note this is very much a guess as it seems very strange to have fields on which the field name is a numeric price.

EDIT - further update based on guesses:-

<div class="adjust">
<center><form class="greatForm" method="post" action="#">
<?php
$read_extra = mysql_query("select id, item_label, price, item_name, item_img from extra_item where theme_name = '$title'");

while($show_extra = mysql_fetch_array($read_extra))
{
    echo "<input type='checkbox' name='checkextra[".$show_extra['id']."]' id='".$show_extra['item_label']."' value='".$show_extra['price']."'/>";
    echo "<label for='".$show_extra['item_label']."' >";
    echo "<img style='height:150px;width:150px;' src='themeinfo/extra/'".$show_extra['item_img']."'/>".$show_extra['item_name']."</label>";
    echo "<input type='hidden' name='item_name[".$show_extra['id']."]' value='".$show_extra['item_name']."'/>";
}

echo "<center><br><br><input type='submit' class='button' style='width:10%;' value='SAVE'  name='choose_extra'></center></br>";
echo "</form></center>";

if(isset($_POST['choose_extra']))
{
    foreach($_POST['checkextra'] as $getextraprice_id=>$getextraprice) 
    {
        $takeextra = mysql_query("INSERT INTO selectextra(user,title,extraitem,price) 
                                VALUES('".mysql_real_escape_string($username)."',
                                    '".mysql_real_escape_string($title)."',
                                    '".mysql_real_escape_string($_POST['item_name'][$getextraprice_id])."',
                                    '".mysql_real_escape_string($getextraprice)."')") or die(mysql_error());

    }
    if($takeextra)
    {
        echo "<center>Extra item order SAVE.</center>";
    }
    else
    {
        echo "failed";
    }
}

?>

</div>

Put out the checkbox with a value of the price, and a hidden field containing the item_name. Both as arrays. The index of these arrays should match. When you process the form you loop through the checkboxes you get the index of each checkbox array item and use that as the index to access the item_name field.

EDIT again. Used a sequence number to force the index of the arrays of form elements (probably should be necessary, but I prefer to force things) rather than using a field from the table.

<div class="adjust">
<center><form class="greatForm" method="post" action="#">
<?php

$conn = mysql_connect('localhost', 'root', '');
mysql_select_db('test', $conn);
$title = 'fred';
$username = 'jo';

$read_extra = mysql_query("SELECT item_label, price, item_name, item_img FROM extra_item WHERE theme_name = '$title'");

$row_cnt = 0;
while($show_extra = mysql_fetch_array($read_extra))
{
    echo "<input type='checkbox' name='checkextra[$row_cnt]' id='".$show_extra['item_label']."' value='".$show_extra['price']."'/>";
    echo "<label for='".$show_extra['item_label']."' >";
    echo "<img style='height:150px;width:150px;' src='themeinfo/extra/'".$show_extra['item_img']."'/>".$show_extra['item_name']."</label>";
    echo "<input type='hidden' name='item_name[$row_cnt]' value='".$show_extra['item_name']."'/>";
    $row_cnt++;
}

echo "<center><br><br><input type='submit' class='button' style='width:10%;' value='SAVE'  name='choose_extra'></center></br>";
echo "</form></center>";

if(isset($_POST['choose_extra']))
{
    foreach($_POST['checkextra'] as $getextraprice_id=>$getextraprice) 
    {
        $takeextra = mysql_query("INSERT INTO selectextra(user,title,extraitem,price) 
                                VALUES('".mysql_real_escape_string($username)."',
                                    '".mysql_real_escape_string($title)."',
                                    '".mysql_real_escape_string($_POST['item_name'][$getextraprice_id])."',
                                    '".mysql_real_escape_string($getextraprice)."')") or die(mysql_error());

    }
    if($takeextra)
    {
        echo "<center>Extra item order SAVE.</center>";
    }
    else
    {
        echo "failed";
    }
}

?>

</div>
share|improve this answer
    
i dont understand mysqli – user3652484 May 19 '14 at 13:23
    
It is virtually the same sql normal mysql_* calls that you are already using. But the main thing to do is to add the debugging so we have some idea of what the error is that your code is failing with – Kickstart May 19 '14 at 13:26
    
but my problem is how to insert checkbox name and value to database. The checkbox is generate by while loop. – user3652484 May 19 '14 at 13:34
    
And you say that it comes out with the message saying 'failed'. But that doesn't say WHY it failed (hence the extra code to put out the mysql error messages). Once output you do not know what the checkbox names are to read the values in, and the easiest way to process them would be in a loop, but you have commented that "question said while loop checkbox not while loop insert". If you want to process the checkboxes already checked then the best place to do that would be in the loop that outputs the checkboxes, as then you know the names of the fields output. – Kickstart May 19 '14 at 13:45
    
actually the failed is echo, if query not insert echo failed. I have updated my question a bit. I put the checkbox name to name="checkextra[]" and now it give me this *Notice: Array to string conversion in D:\xampp\htdocs* – user3652484 May 19 '14 at 14:01

First have to put $_SESSION['itemname'] = array(); on top of the html

<div class="adjust">
<center><form class="greatForm" method="post" action="#">
<?php
$read_extra = mysql_query("select * from extra_item where theme_name = '$title'");
$a = 1;
while($show_extra = mysql_fetch_array($read_extra))
{
    $_SESSION['getextraname'] = $show_extra['item_name'];

?>
    <input type="checkbox" name="checkextra<?php echo $a?>" id="<?php echo   $show_extra['item_label']; ?>" value="<?php echo $show_extra['price']; ?>"/>
    <label for="<?php echo $show_extra['item_label']; ?>" >
    <img style="height:150px;width:150px;" src="<?php echo 'themeinfo/extra/'.$show_extra['item_img']; ?>"/><?php echo $show_extra['item_name']; ?></label>
    <input type="hidden" name="getextraname<?php echo $a?>" value="<?php echo   $show_extra['item_name']; ?>"/>

<?php
$a = $a+1;
}
 ?>
 <center><br><br><input type="submit" class="button" style="width:10%;" value="SAVE" name="choose_extra"></center></br>
</form></center>
<?php


    if(isset($_POST['choose_extra']))
 {


    $number = mysql_query("select COUNT(*) as num from extra_item where  theme_name = '$title'");
    $number_data = mysql_fetch_array($number);

    for($x=1;$x<=$number_data['num'];$x++)
    {
        if(isset($_POST['checkextra'.$x]))
        {
            array_push($_SESSION['itemname'],$_POST['getextraname'.$x]);
        }

    }


    foreach($_SESSION['itemname'] as $keyname)
    {
        $redname = mysql_query("select * from extra_item where theme_name = '$title' and item_name='$keyname'");
        $takename = mysql_fetch_array($redname);
        $getextraname = $takename['item_name'];
        $getextraprice = $takename['price'];

        $saveallextra = mysql_query("insert into selectextra(user,title,extraitem,price) values('$username','$title','$getextraname',$getextraprice)");

        if($saveallextra)
            {
                echo "<center>Extra item order SAVE.</center>";
            }
            else{
                echo "Failed";
            }

    }

}

?>

</div>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.