Jump to content

insert radio and checkbox values into mysqli d.b.


erok

Recommended Posts

Hi everyone,
i am unable to insert radio and checkbox values into phpmyadmin d.b. columns all i see is zero values in columns.
what am i doing wrong?
Any help greatly appreciated.
erok
<form enctype="multipart/form-data" method="post" action="deneme.php" >
<span>Title</span><input type="text" name="title" size="78" maxlenght="78" value="available" />
<br />
<span>Transportation</span>Yes  <input type="radio" name="trans" size="3" maxlenght="3" <?php if (isset($trans) && $trans=="yes") echo "checked";?> value="yes" />
No <input type="radio" name="trans" size="3" maxlenght="3" <?php if (isset($trans) && $trans=="no") echo "checked";?> value="no" />
<br />
<span>**** Lenght(s)</span>upto 50** <input type="checkbox" name=" lenght[]" size="9" maxlenght="9" value="50" />
upto 100** <input type="checkbox" name="lenght[]" size="9" maxlenght="9" value="100" />
upto 150** <input type="checkbox" name="lenght[]" size="9" maxlenght="9" value="150" />
<br />
<span> </span>upto 200** <input type="checkbox" name="lenght[]" size="9" maxlenght="9" value="200" />
over 200** <input type="checkbox" name="lenght[]" size="9" maxlenght="9" value="201" />
<br />
<span>**** Period</span>Transient <input type="checkbox" name="period[]" size="9" maxlenght="9" value="trans" />
Short Term <input type="checkbox" name="period[]" size="9" maxlenght="9" value="short" />
long Term <input type="checkbox" name="period[]" size="9" maxlenght="9" value="long" />
<br />
<input type="submit" name="kitchen" id="forminsert" value="upload all" size="35" />.
<br />
</form>
<?php
error_reporting(E_ALL | E_STRICT) ;
ini_set('display_errors', 'on');
ini_set('html_errors', true);
//connect to database server
$con = mysqli_connect("localhost", "root", "");
if (mysqli_connect_error($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error() ;
}
// select database
mysqli_select_db($con, "lima") or die("Could not find DB");
$title = $trans = $lenght = $period = "" ;
if(!empty($_POST['title'])) {
$title=$_POST['title'] ;
}
echo $title ;
if(!empty($_POST['trans'])) {
$trans=$_POST['trans'] ;
}
echo $trans ;
if(!empty($_POST['lenght']))
{
$lenght=array() ;
$lenght1=$_POST['lenght'] ;
foreach($lenght1 as $value1) {
$lenght=$value1 ;
}
}
echo $lenght ;
if(!empty($_POST['period']))
{
$period=array() ;
$period=$_POST['period'] ;
foreach($period as $value3) {
$period=$value3 ;
}
}
echo $period ;
// insert into table
$sql = "insert into dakdeneme (title, trans, lenght, period) values ('{$title}', '{$trans}', '{$lenght}', '{$period}')" ;
$sql= mysqli_query($con, $sql) ;
?>
Link to comment
Share on other sites

You've misspelled "length" which is going to make your HTML not work right.

 

Your code is open to hacking by SQL injection.

 

This block of code has multiple problems:

$period=array();$period=$_POST['period'] ;foreach($period as $value3) {     $period=$value3 ;  }

For one, there's no point in setting $period to an array when you're overwriting it in the next line.

Your foreach loop is going to end up with $period not being an array and only containing the very last value. Modifying the value right in the loop will probably cause the loop to end early, since $period is no longer an array. Something like this is probably closer to what you were looking for:

$period = array();foreach($_POST['period'] as $value3) {   $period[] = $value3;}

Now you have another problem: $period is an array, you can't just pass that to the query, because all that would be stored is the string "Array". I don't actually know what exactly you're trying to store in the database. You only have one database field for period, but you have multiple different values that can be selected for it at the same time by using checkboxes. Were you actually intending to use radio buttons?

Link to comment
Share on other sites

Thank you very much for analyzing my codes.

I searched on several web sites but my mind is not clear about how checkboxes and radio buttons works, how to insert them into database. Maybe value 1,2,3.. or yes,no

i want user would be able to, e.g. check period checkbox short and long together and code would be able to store in database period column. Yes i meant to use checkbox for period.

Link to comment
Share on other sites

I want to display that particular property is available for short term or long term or both short term and long term available depends on property owner's preferences. Is it possible to store more than one value in one column?

Other thing i assumed when i set $period=array(), $period=$value3 would be array not string. Sounds like i was wrong.
Link to comment
Share on other sites

That's not how assignment works. Every time you write $period = something you're overwriting anything it might have contained before.

 

There are multiple possible solutions for the database

  • One solution, which would allow for searches, would be to have a separate database table that associates each item with a value "short" or "long". If it has both values then there will be two rows in the table, one row associating it with "short" and the other row associating it with "long"
  • Another option would be to have a third value "both" and store that in the table when both of them are selected.
  • The third option would be to have PHP implode the array of values so that it would show up in the database table as a string with values separated by commas

Link to comment
Share on other sites

this code is able to
if(!empty($_POST['period']))
{
$period=array() ;
$period=implode(",", $_POST['period']) ;
echo $period ;
}
echo out 100,200 on the screen when i checked both 100 and 200. But only insert value 100 to database.
this code is able to insert into database phpmyadmin as value 0 or 1 whichever i check. Value "yes" or "no" unable to insert into database.
if(!empty($_POST['trans'])) {
$trans=$_POST['trans'] ;
}
echo $trans ;
Yes  <input type="radio" name="trans" size="3" maxlength="3" value="1" /> instead of value="yes"
No <input type="radio" name="trans" size="3" maxlength="3" value="0" /> instead of value="no"
Link to comment
Share on other sites

That's because your database field only accepts integers, you can't pass a string with a comma in it because it will truncate it. No matter what solution you want to try, you're going to need to change your database table structure.

Link to comment
Share on other sites

True tinyint set. That is why excepts integer. But still " echo out ' 100,200 ' on the screen when i checked both 100 and 200. But only insert value 100 to database."

how can i iterate in to the same field? implode helps only for displaying on the screen both values but not inserting into database.

Link to comment
Share on other sites

What exactly are you sending to the database? Print out your query. If it looks like this:

INSERT INTO table (field) VALUES ('100,200')
and your field is an integer, then yeah it's going to convert the string "100,200" to an integer, get 100, and insert that. If you're trying to insert multiple values into the database in multiple rows then you need to loop through your values and insert each one with a separate query, or build a single multiple-insert query.
Link to comment
Share on other sites

My question is ;
How can i iterate into the same field different checkbox values? Implode function is good for displaying both values on the screen, but not inserting both values into same database field . Only inserting the first value in the checkbox to the field, ignores the the second or third...values.
this code is able to
if(!empty($_POST['period']))
{
$period=array() ;
$period=implode(",", $_POST['period']) ;
echo $period ;
}

echo out 100,200 on the screen when i checked both 100 and 200. But only insert value 100 to database. Values are integer not string.

 

and looks like this;

// insert into table

$sql = "insert into dakdeneme (title, trans, length, period) values ('{$title}', '{$trans}', '{$length}', '{$period}')" ;
$sql= mysqli_query($con, $sql) ;
I want to display that particular boat is available for 100 days or 200 days or both 100 and/or 200 days depends on boat owner's preferences.
Link to comment
Share on other sites

It sounds like you need a different database design. Obviously a single field that holds an integer cannot hold 2 different integers at the same time. If you have multiple records like that then you have a 1-to-many relationship and you need to use a separate table to store that data. One table would store the information about the boat itself and the other table would have a list of the days or periods that it is available along with the boat ID to link those records back to the boat. That's a standard 1-to-many relationship in SQL. One boat may have many lengths or periods.The other option is to use a string data type instead of integer and then just store a list of the values, but the problem with a design like that is that you lose SQL's ability to quickly search for records if you're storing multiple searchable values in a single field.

Link to comment
Share on other sites

"The other option is to use a string data type instead of integer and then just store a list of the values, but the problem with a design like that is that you lose SQL's ability to quickly search for records if you're storing multiple searchable values in a single field. "

I think i m willing to use a string data type instead of focusing on search speed.

Link to comment
Share on other sites

I faced a setback when i increase number of columns from 5 to 31 combination of radios, checkboxes and text, plus some images which are going to another table under the same form. I can not upload to database tables. No error messages. Is it because limitation of xampp? is it too much for xampp to able to handle?

Link to comment
Share on other sites

Tinyint(4) for radiobuttons columns

varchar for checkboxes and text columns.

same structure i used earlier with which i can upload to phpmyadmin database table. Only difference more columns.

I have not tried to upload images yet, images gonna go to another table though.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...