I have code for shuffling images and display.Now i want to values of checked boxes will store on database.please help

<?php
mysql_connect("localhost","root","");
mysql_select_db("impass");
?>
<html>
<body>
<form action="" method="post">


<?php
$images = array(
            '<input name="chk1" type="checkbox" value="1" /><img src="images/images (4).jpg" alt="" width="234" height="212" />',
            '<input name="chk2" type="checkbox" value="2"  /><img src="images/images (6).jpg" alt="" width="234" height="212" />',
             '<input name="chk3" type="checkbox" value="3" /><img src="images/images (5).jpg" alt="" width="234" height="212" />', '<input name="chk4" type="checkbox" value="4" /><img src="images/drt.jpg" alt="" width="234" height="212" />', '<input name="chk5" type="checkbox" value="5" /><img src="images/rf.jpg" alt="" width="234" height="212" />', '<input name="chk6" type="checkbox" value="6" /><img src="images/yu.jpg" alt="" width="234" height="212" />', '<input name="chk7" type="checkbox" value="7" /><img src="images/ed.jpg" alt="" width="234" height="212" />'
        );

    shuffle($images); // Randomize images array;
    echo $images[0];
    echo $images[1];
    echo $images[2];

    ?>
<br /><br/><input name="s" type="submit" value="Submit Query" />
</form>
</body>
</html>
<?php
if(isset($_POST['submit']))
{
    $chk1=isset($_POST['chk1']);
    $chk2=isset($_POST['chk2']);
    $chk3=isset($_POST['chk3']);
    $chk4=isset($_POST['chk4']);
    $chk5=isset($_POST['chk5']);
    $chk6=isset($_POST['chk6']);
    $chk7=isset($_POST['chk7']);
    $ins=mysql_query("insert into(images,image2,image3,image4,image5,image6,image7) values('$chk1','$chk2','$chk3','$chk4','$chk5','$chk6','$chk7')");

if($ins>0)
{ echo "sucess";
}
else
{
    echo mysql_error();
}
}
?>
Member Avatar

I'm assuming that your DB table is not quite right. Very difficult to maintain if you need to change number of images. Anyway, can't speculate too much as I jave no idea of the use. Don't use mysql_* functions either as they've been deprecated - use PDO or mysqli.

$images = array(
            '<input name="chk1" type="checkbox" value="1" /><img src="images/images (4).jpg" alt="" width="234" height="212" />',

..etc. This is a bit wasteful and difficult to maintain. You could do something like:

$images = array(
    array(1,'images (4).jpg'),
    array(2,'images (6).jpg'),
    array(3,'images (5).jpg'),
    array(4,'drt.jpg'),
    array(5,'rf.jpg'),
    array(6,'yu.jpg'),
    array(7,'ed.jpg')
);

$width = 234;
$height = 212;

$imagesToDisplay = 3;

shuffle($images);

for($x=0;$x<$imagesToDisplay;$x++)
{
    echo "<input name='chk{$images[$x][0]}' type='checkbox' value='{$images[$x][0]}' /><img src='{$images[$x][1]}' alt='' width='$width' height='$height' />
}

That allows you to change your data easily without having to go through the whole stored markup.

Your form handling code (which should be in a different file), but anyway...

if(isset($_POST['submit']))
{
    $chk1=isset($_POST['chk1']);
    $chk2=isset($_POST['chk2']);
    $chk3=isset($_POST['chk3']);
    //[..etc..]
    $ins=mysql_query("insert into(images,image2,image3,image4,image5,image6,image7) values('$chk1','$chk2','$chk3','$chk4','$chk5','$chk6','$chk7')");

That won't work as $chk1 for example will not exist if $_POST['chk1'] does not exist in your form or is not checked. So this will throw an error when you try to use it in your SQL query. So, you need to consider either not including them in your SQL or provide them with a NULL value...

    $chk1=(isset($_POST['chk1'])) ? $_POST['chk1'] : NULL;
    $chk2=(isset($_POST['chk2'])) ? $_POST['chk2'] : NULL;
    $chk3=(isset($_POST['chk3'])) ? $_POST['chk3'] : NULL;

etc.

Having said all that, I am totally lost as to why you're storing this data like that. So images will always be empty or 1, images2 will always be empty or 2? Seems a bit pointless. A boolean or tinyint(1) field may be easier - true/false.

Plz change if(isset($_POST['submit'])) to if(isset($_POST['s'])). Bcoz your button name is "s"

And also check your insert query syntax,

plus, you can rewrite this

if($ins>0){ 
    echo "sucess";
}

to something like this

if($ins){ 
    echo "sucess";
}

because anything greater than zero are always presumed to be true.