hya i have created a table of multi checkbox group and added a [] at the end of the name in html now when i check a selection of boxes i just get array echoed what do i do to sort it out and get the list of items that i have ticked x
janicemurby 7 Posting Whiz in Training
cereal 1,524 Nearly a Senior Poster Featured Poster
Yes, but if you can give more details we can suggest proper solutions... just to consider a possible scenario: if these are going to be used in a search query you can convert them into a CSV string and use the MySQL FIND_IN_SET()
function:
For example:
<?php
$pdo = new PDO('dsn', 'user', 'pass');
$items = implode(',', $_POST['items']);
$stmt = $pdo->prepare("SELECT * FROM records WHERE FIND_IN_SET(id, ?)");
$stmt->execute([$items]);
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
print_r($result);
About PDO:
janicemurby 7 Posting Whiz in Training
i have enclosed a sample from my tut book not site due to adult content but here is what i have so far
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html" />
<meta name="author" content="Kevin Robinson" />
<title>An html form with checkboxes</title>
</head>
<body>
<form action="formwithcb.php" method="POST">
<p><label>Name:</label><br/>
<fieldset>
<legend>Select some products</legend><br />
<input type="checkbox" id="tricorder" name="products" value="tricorder"/><label>Tricoder</label><br />
<input type="checkbox" id="orac_ai" name="products" value="orac_ai"/><label>orac_ai</label><br />
<input type="checkbox" id="hal_2000" name="products" value="hal_2000"/><label>hal_2000</label><br />
</fieldset>
<button type="submit" name="submit" value="submit">Submit Form</button>
</form>
</body>
</html>
then php i have
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html" />
<meta name="author" content="Kevin Robinson" />
<title>Reading Check Boxes</title>
</head>
<body>
<p>Welcome, <strong><?php echo $_POST['user']; ?></strong>!</p>
<p>Your product Choices Are:
<?php
if (!empty($_POST['products'])) {
echo "<ul>";
foreach ($_POST['products'] as $value) {
echo "<li>$value</li>";
}
echo "<ul>";
} else {
echo "none";
}
?>
</body>
</html>
and i get the following error
Warning: Invalid argument supplied for foreach() in /home/a2085360/public_html/checkbox/formwithcb.php on line 15
janicemurby 7 Posting Whiz in Training
sorted error out x
janicemurby 7 Posting Whiz in Training
but when i put the php section in my profile page as in
<p>Your Interests Are:
<?php
if (!empty($_POST['interests'])) {
echo "<ul>";
foreach ($_POST['interests'] as $value) {
echo "<li>$value</li>";
}
echo "<ul>";
} else {
echo "none";
}
?>
i get you have not selected anything and when i check database column for interests i just get array printed in it
cereal 1,524 Nearly a Senior Poster Featured Poster
This because at the moment you're just looping the received array, I don't see any variable that could return a database result set. Can you show how you build the WHERE
conditions and the final query?
janicemurby 7 Posting Whiz in Training
hya hun this is the php from editprofile.php
<?php
include("config/db_connect.php");
// Check user id
if($_SESSION['userid'] == "")
{
header("location: index.php");
}
// Fetch user details
$userdata = mysql_query("select * from user where user_id = '".$_SESSION['userid']."' ");
$fetch_user = mysql_fetch_array($userdata);
$user_country = $fetch_user['user_country'];
$userinfo = mysql_query("select * from user_info where user_id = '".$fetch_user['user_id']."' ");
$fetch_info = mysql_fetch_array($userinfo);
// Update user Profile
if(isset($_POST['submitform']))
{
$unionterritory = addslashes($_POST['territory']);
$talking_about = addslashes($_POST['talking_about']);
$headline = addslashes($_POST['headline']);
$query_update = mysql_query(" update user_info set seeking = '".$_POST['seeking']."' ,
city = '".$_POST['city']."' ,
union_territory = '".$unionterritory."' ,
postalcode = '".$_POST['postalcode']."' ,
height = '".$_POST['height']."' ,
bodytype = '".$_POST['bodytype']."' ,
bodyhair = '".$_POST['bodyhair']."' ,
hair = '".$_POST['haircolor']."' ,
eyecolor = '".$_POST['eyecolor']."' ,
dosmoke = '".$_POST['dosmoke']."' ,
dodrink = '".$_POST['dodrink']."' ,
tattoos = '".$_POST['tattoos']."' ,
travel = '".$_POST['travel']."' ,
piercings = '".$_POST['piercings']."' ,
rate1 = '".$_POST['rate1']."' ,
rate2 = '".$_POST['rate2']."' ,
rate3 = '".$_POST['rate3']."' ,
rate4 = '".$_POST['rate4']."' ,
rate5 = '".$_POST['rate5']."' ,
rate6 = '".$_POST['rate6']."' ,
rate7 = '".$_POST['rate7']."' ,
talking_about = '".$talking_about."' ,
interests = '".$_POST['interests']."' ,
headline = '".$headline."' where user_id = '".$_SESSION['userid']."' ") or die(mysql_error());
if($query_update)
{
header("location: profile.php");
exit;
}
}
?>
and here is the php for profile.php
<?php
include("config/db_connect.php");
// Fetch User Details for display profile
$query = mysql_query("select * from user where user_id = '".$_SESSION['userid']."'");
$fetch_user = mysql_fetch_array($query);
$user_country = $fetch_user['user_country'];
$user_gender = $fetch_user['user_gender'];
$user_birthdate = $fetch_user['user_birthdate'];
$user_country = $fetch_user['user_country'];
$info_query = mysql_query("select * from user_info where user_id = '".$fetch_user['user_id']."' ");
$fetch_info = mysql_fetch_array($info_query);
$user_info = $fetch_info['union_territory'];
$user_image = mysql_query("select * from user_images where user_id = '".$fetch_user['user_id']."' and main_image = '1' ");
$fetch_image = mysql_fetch_array($user_image);
?>
<h2><?php echo ucfirst($fetch_user['user_name']);?>'s Profile</h2>
<?php if($_GET['err']!='') { ?>
<br />
<p class="red" align="center"><strong><?php echo $_GET['err']; ?></strong></p>
<?php } ?>
<br />
<?php if(trim($fetch_info['headline']) !='' ){
$headline = $fetch_info['headline'];
} else {
$headline = "Looking For ".$fetch_info['lookingfor'];
} ?>
<?php echo $headline;?>
<p> </p>
<hr />
<p> </p>
<table>
<tr>
<td><?php echo $fetch_user['user_gender'];?></td><td> </td><td> </td><td><?php echo $fetch_info['city'];?></td><td> </td><td> </td><td><?php echo $fetch_info['union_territory'];?></td>
</tr>
</table>
<p> </p>
<p> </p>
<?php echo stripslashes($fetch_info['talking_about']);?>
<p> </p>
<p> </p>
<table>
<tr>
<td><?php
$sql_slide = mysql_query("select * from user_images where user_id = '".$fetch_user['user_id']."' and show_profile = '1' ");
if(mysql_num_rows($sql_slide)>1) {
while($row_slide = mysql_fetch_array($sql_slide))
{
?>
<br />
<img src="images/user_images/smallthumb/<?php echo $row_slide['user_image'];?>" border="0" width="100" height="100" data-tooltip="sticky<?php echo $row_slide['id'];?>" />
<?php
} }
?>
<?php
$sql_slideh = mysql_query("select * from user_images where user_id = '".$fetch_user['user_id']."' and show_profile = '1' ");
while($row_slideh = mysql_fetch_array($sql_slideh))
{
?>
<?php if($row_slideh['caption']!='') { ?>
<?php } ?>
<?php } ?>
<?php if($fetch_image['user_image']!='') {?>
<img src="images/user_images/<?php echo $fetch_image['user_image'];?>" height="100px;" width="100px;" />
<?php } else { ?>
<img src="images/blank_big.jpg" height="100px;" width="100px;" />
<?php } ?></td><td><?php echo ucfirst($fetch_user['user_name']);?>
<br /><label>Last Logged in:</label><?php $then = new DateTime($lastlogin['lastlogin']);
$now = new DateTime("now");
echo $then->diff($now)->format('%a days'); ?> ago
<br /><label>Member Since:</label><?php echo $fetch_user['reg_date'];?></td>
</tr>
</table>
<p> </p>
<h1>Personal Details</h1>
<p> </p>
<table>
<tr>
<td><label>Age: </label><?php echo Age($user_birthdate);?> year old,</td>
</tr>
<tr>
<td><label>Sexuality: </label><?php echo $fetch_info['sexuality'];?></td>
</tr>
<tr>
<td><label>Height: </label><?php echo $fetch_info['height'];?>
cm (<?php echo get_height($fetch_info['height'])?>) height,</td>
</tr>
<tr>
<td><label>Body type: </label><?php echo $fetch_info['bodytype'];?></td>
</tr>
<tr>
<td><label>Body Hair: </label><?php echo $fetch_info['bodyhair'];?></td>
</tr>
<tr>
<td><label>Eye Color: </label><?php echo $fetch_info['eyecolor'];?></td>
</tr>
<tr>
<td><label>Hair Color: </label><?php echo $fetch_info['hair'];?></td>
</tr>
<tr>
<td><label>Ethnicity: </label><?php echo $fetch_user['user_ethnicity'];?></td>
</tr>
<tr>
<td><label>Smoke: </label><?php echo $fetch_info['dosmoke'];?></td>
</tr>
<tr>
<td><label>Drinker: </label><?php echo $fetch_info['dodrink'];?></td>
</tr>
<tr>
<td><label>Tattoos: </label><?php echo $fetch_info['tattoos'];?></td>
</tr>
<tr>
<td><label>Piercings: </label><?php echo $fetch_info['piercings'];?></td>
</tr>
</table>
<h1>Escorting Details</h1>
<p> </p>
<table>
<tr>
<td><label>I am Seeking a: </label><?php echo $fetch_info['seeking'];?></td>
</tr>
<tr>
<td><label>incalls: </label><?php echo $fetch_info['incalls'];?></td>
</tr>
<tr>
<td><label>outcalls: </label><?php echo $fetch_info['outcalls'];?></td>
</tr>
<tr>
<td><label>I will travel: </label><?php echo $fetch_info['travel'];?></td>
</tr>
</table>
<br/>
<hr />
<br/>
<table>
<tr>
<td><span class="style3">15 Mins</span></td><td> </td>
<td><span class="style3">30 Mins</span></td><td> </td>
<td><span class="style3">1 Hour</span></td><td> </td>
<td><span class="style3">2Hours</span></td><td> </td>
<td><span class="style3">3 Hours</span></td><td> </td>
<td><span class="style3">4 Hours</span></td><td> </td>
<td><span class="style3">Overnight</span></td>
</tr>
<tr>
<td><?php echo $fetch_info['rate1']?></td><td> </td>
<td><?php echo $fetch_info['rate2']?></td><td> </td>
<td><?php echo $fetch_info['rate3']?></td><td> </td>
<td><?php echo $fetch_info['rate4']?></td><td> </td>
<td><?php echo $fetch_info['rate5']?></td><td> </td>
<td><?php echo $fetch_info['rate6']?></td><td> </td>
<td><?php echo $fetch_info['rate7']?></td><td> </td>
</tr>
</table>
<table>
<p> </p>
<h1>Interests</h1>
<p> </p>
<p>Your Interests Are:
<?php
if (!empty($_POST['interests'])) {
echo "<ul>";
foreach ($_POST['interests'] as $value) {
echo "<li>$value</li>";
}
echo "<ul>";
} else {
echo "none";
}
?>
cereal 1,524 Nearly a Senior Poster Featured Poster
Ok, I see the problem, in the update query that starts at line 25
you have this:
interests = '".$_POST['interests']."' ,
If $_POST['interests']
was a string then this would work fine, but it's an array, so you have to implode it:
$interests = implode(',', $_POST['interests']);
Then the update query will save the values:
interests = '".$interests."' ,
Then, when you retrieve the results from user_info
table and save them into $fetch_info
, you can decide to display them directly as comma separated values, or you explode them:
$interests = explode(',', $fetch_info['interests']);
So you can now loop the array:
echo "<ul>";
foreach($interests as $interest)
{
echo "<li>$interest</li>";
}
echo "</ul>";
Oh, regarding your queries you should really use prepared statements, at the moment your queries can be manipulated through SQL injections, read about PDO or MySQLi:
Consider also filter_input()
to sanitize and validate the input you receive from users, never trust what is sent from clients:
Hope it helps, bye!
Edited by cereal
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.