Can't insert data to Database with PHP - php

Can't make basic things. The connection to database is good, cause it shows list, deletes data lines. Also after submiting, it doesn't show any errors, but it doesn't show in database...I am still a beginner in PHP, I searched thoroughly but I can't find out what's wrong in code
This is my model class:
<?php
class abonimentas {
private $abonimentas_lentele = '';
public function __construct() {
$this->abonimentas_lentele = config::DB_PREFIX . 'abonimentas';
}
public function getAbonimentasListCount() {
$query = " SELECT COUNT(`{$this->abonimentas_lentele}`.`id`) as `kiekis`
FROM `{$this->abonimentas_lentele}`";
$data = mysql::select($query);
return $data[0]['kiekis'];
}
public function getAbonimentas($id) {
$query = " SELECT *
FROM `{$this->abonimentas_lentele}`
WHERE `id`='{$id}'";
$data = mysql::select($query);
return $data[0];
}
public function insertAbonimentas($data) {
$query = " INSERT INTO `abonimentas`
(
`pavadinimas`,
`aprasymas`,
`papildomos_salygos`,
`kaina`
)
VALUES
(
'{$data['pavadinimas']}',
'{$data['aprasymas']}',
'{$data['papildomos_salygos']}'
'{$data['kaina']}'
)";
mysql::query($query);
return mysql::getLastInsertedId();
}
public function updateAbonimentas($data) {
$query = " UPDATE `abonimentas`
SET `pavadinimas`='{$data['pavadinimas']}',
`aprasymas`='{$data['aprasymas']}',
`papildomos_salygos`='{$data['papildomos_salygos']}',
`kaina`='{$data['kaina']}'
WHERE `id`='{$data['id']}'";
mysql::query($query);
}
}
This is creation class:
<?php
include 'libraries/abonimentas.class.php';
$abonimentasObj = new abonimentas();
$formErrors = null;
$data = array();
$required = array('pavadinimas', 'aprasymas', 'papildomos_salygos', 'kaina');
$maxLengths = array (
'pavadinimas' => 40,
'aprasymas' => 200,
'papildomos_salygos' => 200
);
if(!empty($_POST['submit'])) {
// nustatome laukų validatorių tipus
$validations = array (
'pavadinimas' => 'anything',
'aprasymas' => 'anything',
'papildomos_salygos' => 'anything',
'kaina' => 'price');
include 'utils/validator.class.php';
$validator = new validator($validations, $required, $maxLengths);
if($validator->validate($_POST)) {
$dataPrepared = $validator->preparePostFieldsForSQL();
$dataPrepared['id'] = $abonimentasObj->insertAbonimentas($dataPrepared);
$abonimentasObj->updateAbonimentas($dataPrepared);
header("Location: index.php?module={$module}&action=list");
die();
} else {
$formErrors = $validator->getErrorHTML();
$data = $_POST;
}
}
include 'templates/abonimentas_form.tpl.php';
?>
This is template for input:
<ul id="pagePath">
<li>Pradžia</li>
<li>Abonimentai</li>
<li><?php if(!empty($id)) echo "Abonimento redagavimas"; else echo "Naujas abonimentas"; ?></li>
</ul>
<div class="float-clear"></div>
<div id="formContainer">
<?php if($formErrors != null) { ?>
<div class="errorBox">
Neįvesti arba neteisingai įvesti šie laukai:
<?php
echo $formErrors;
?>
</div>
<?php } ?>
<form action="" method="post">
<fieldset>
<legend>Abonimento informacija</legend>
<p>
<label class="field" for="pavadinimas">Pavadinimas<?php echo in_array('pavadinimas', $required) ? '<span> *</span>' : ''; ?></label>
<input type="text" id="pavadinimas" name="pavadinimas" class="textbox textbox-200" value="<?php echo isset($data['pavadinimas']) ? $data['pavadinimas'] : ''; ?>">
<?php if(key_exists('pavadinimas', $maxLengths)) echo "<span class='max-len'>(iki {$maxLengths['pavadinimas']} simb.)</span>"; ?>
</p>
<p>
<label class="field" for="aprasymas">Aprašymas<?php echo in_array('aprasymas', $required) ? '<span> *</span>' : ''; ?></label>
<textarea id="aprasymas" name="aprasymas" class=""><?php echo isset($data['aprasymas']) ? $data['aprasymas'] : ''; ?></textarea>
<?php if(key_exists('aprasymas', $maxLengths)) echo "<span class='max-len'>(iki {$maxLengths['aprasymas']} simb.)</span>"; ?>
</p>
<p>
<label class="field" for="papildomos_salygos">Papildomos sąlygos<?php echo in_array('papildomos_salygos', $required) ? '<span> *</span>' : ''; ?></label>
<textarea id="papildomos_salygos" name="papildomos_salygos" class=""><?php echo isset($data['papildomos_salygos']) ? $data['papildomos_salygos'] : ''; ?></textarea>
<?php if(key_exists('papildomos_salygos', $maxLengths)) echo "<span class='max-len'>(iki {$maxLengths['papildomos_salygos']} simb.)</span>"; ?>
</p>
<p>
<label class="field" for="kaina">Kaina<?php echo in_array('kaina', $required) ? '<span> *</span>' : ''; ?></label>
<input type="number" id="kaina" name="kaina" class=""><?php echo isset($data['kaina']) ? $data['kaina'] : ''; ?></input>
<?php if(key_exists('kaina', $maxLengths)) echo "<span class='max-len'>(iki {$maxLengths['kaina']} simb.)</span>"; ?>
</p>
</fieldset>
<p class="required-note">* pažymėtus laukus užpildyti privaloma</p>
<p>
<input type="submit" class="submit button" name="submit" value="Išsaugoti">
</p>
<?php if(isset($data['id'])) { ?>
<input type="hidden" name="id" value="<?php echo $data['id']; ?>" />
<?php } ?>
</form>
</div>

Related

Populating checkboxes from database using PHP - only last option is getting checked

I am trying to populate checkboxes with the data from my mysql database but for some reason only the last checkbox is being checked (for example if automotive, carpentry and hand tools should be checked, only hand tools is being checked) and I can't figure out why. The mysql statement is running correctly and giving me the correct information. Here is the relevant code.
<?php
require_once('../../private/initialize.php');
require_login();
if(!isset($_GET['id'])) {
redirect_to(url_for('/members/show_member_tools.php'));
}
$id = $_GET['id'];
if(is_post_request()) {
// Handle form values sent by new.php
$tool = [];
$tool['tool_ID'] = $id;
$tool['serial_number'] = $_POST['serial_number'] ?? '';
$tool['tool_name'] = $_POST['tool_name'] ?? '';
$tool['tool_description'] = $_POST['tool_description'] ?? '';
$tool['tool_picture'] = $_POST['tool_picture'] ?? '';
$category =[];
$category = $_POST['category_ID'];
$result = update_tool($tool, $category);
//get info for checkboxes
global $db;
if($result === true) {
$_SESSION['message'] = "The tool has been updated sucessfully";
redirect_to(url_for('/members/show_tool.php?id=' . $id));
} else {
$errors = $result;
}
} else {
$tool = find_tool_by_id($id);
if(isset($_GET['id'])){
$id=$_GET['id'];
$sql = "select category_name from category INNER JOIN tool_category ON category.category_ID = tool_category.category_ID where tool_category.tool_id=$id";
$query = mysqli_query($db, $sql);
while($row=mysqli_fetch_array($query)) {
// $str = "";
$str = $row['category_name'];
echo $str;
if (strpos($str , "automotive")!== false){
$checked1 ="checked";
echo "made it to automotive";
} else {
$checked1 ="";
}
if (strpos($str , "carpentry")!== false){
$checked2 ="checked";
echo "made it to carpentry";
} else {
$checked2 ="";
}
if (strpos($str , "home maintenance")!== false){
$checked3 ="checked";
echo "made it to home maintenance";
} else {
$checked3 ="";
}
if (strpos($str , "plumbing")!== false){
$checked4 ="checked";
} else {
$checked4 ="";
}
if (strpos($str , "yard and garden")!== false){
$checked5 ="checked";
} else {
$checked5 ="";
}
if (strpos($str , "hand tools")!== false){
$checked6 ="checked";
} else {
$checked6 ="";
}
}//end while loop
} //end if
} //end else
$tool_set = find_all_tools();
$tool_count = mysqli_num_rows($tool_set);
mysqli_free_result($tool_set);
?>
<?php $page_title = 'Edit Tool'; ?>
<?php include(SHARED_PATH . '/header.php'); ?>
<div id="content">
<div class="center">
« Back to My Tools
<h2>Edit Tool</h2>
</div>
<?php echo display_errors($errors); ?>
<form action="<?php echo url_for('/members/edit_tool.php?id=' . h(u($id))); ?>" method="post">
<fieldset class="form">
<img src ="<?php echo h($tool['tool_picture']); ?>" alt="<?php echo h($tool['tool_picture']); ?>"width="150"><br>
<label for="serial_number">Serial Number</label><br>
<input type="text" name="serial_number" value="<?php echo h($tool['serial_number']); ?>" ><br>
<label for="tool_name">Tool Name</label><br>
<input type="text" name="tool_name" value="<?php echo h($tool['tool_name']); ?>" ><br>
<label for="tool_description">Tool Description</label><br>
<input type="text" name="tool_description" value="<?php echo h($tool['tool_description']); ?>" ><br>
<label for="category_ID">Tool Category: </label><br>
<input type="checkbox" name="category_ID[]" value="1" <?php echo $checked1; ?>> <label for="1">Automotive</label> <br>
<input type="checkbox" name="category_ID[]" value="2" <?php echo $checked2; ?>> <label for="2">Carpentry</label> <br>
<input type="checkbox" name="category_ID[]" value="3" <?php echo $checked3; ?>> <label for="3">Home Maintenance</label> <br>
<input type="checkbox" name="category_ID[]" value="4" <?php echo $checked4; ?>> <label for="4">Plumbing </label><br>
<input type="checkbox" name="category_ID[]" value="5" <?php echo $checked5; ?>> <label for="5">Yard and Garden</label> <br>
<input type="checkbox" name="category_ID[]" value="6" <?php echo $checked6; ?>> <label for="6">Hand Tools</label> <br>
<input type="submit" value="Edit Tool" >
<a class="block" href="<?php echo url_for('/members/delete_tool.php?id=' . $id); ?>">Delete Tool</a>
</fieldset>
</form>
<div class="push"></div>
</div>
<?php include(SHARED_PATH . '/footer.php'); ?>
You're looping over your results. This means with every loop you're setting one variable to "checked" and the rest to an empty string. So only the last one will be checked. The band-aid fix is to set unchecked as the default outside of the loop, and then change to checked only when it's needed.
But the real fix is to be pulling this info from the database and working with it instead of manually mapping database IDs to labels. By moving your condition into the join, you pull all the categories. The rows that have a tool ID are checked, and the others are not. You're also pulling the category names and IDs so you can programmatically build your checkboxes.
See here for DB sample: http://sqlfiddle.com/#!9/20b223/14/0
$tool = find_tool_by_id($id);
$tool["categories"] = [];
$sql = "SELECT c.category_name, c.category_ID, tc.tool_id
FROM category c
LEFT JOIN tool_category tc ON c.category_ID = tc.category_id
AND tc.tool_id = ?";
$stmt = $db->prepare($sql);
$stmt->bind_param("i", $_GET["id"]);
$result = $stmt->execute();
while($row = $stmt->fetch_assoc()) {
$id = $row["category_ID"];
$name = $row["category_name"];
$checked = $row["tool_id"] ? "checked" : "";
$tool["categories"][$id] = ["name" => $name, "checked" => $checked];
}
Now later on you can do this to automatically build all your checkbox inputs:
<?php foreach ($tool["categories"] as $id=>$category): ?>
<input type="checkbox" name="category_ID[]" id="category_<?=$id?>" value="<?=$id?>" <?=$category["checked"]?>>
<label for="category_<?=$id?>">
<?=htmlspecialchars($category["name"])?>
</label><br/>
<?php endforeach ?>

How to post only variable with values and ignore blank inputs in PHP?

Forms input value variables are the same changing only labels for it from DB. When form selected other ones is hidden with "style.display = "none".
The problem that I face how to prevent sending empty values from none visible input fields?
Tried a trim function but didn't work for me. Maybe didn't use it correctly.
I'm trying to accomplish that input value would stay the same regardless of what user would select and only labels would change.
dump output
<?php
if (isset($_POST["save"])) {
if (empty($_POST['price'])) {
$error["price"] = "Price Required";
} else {
$price = $_POST["price"];
}
if (array_filter($error)) {
echo "Input error";
} else {
$price = ($_POST['price']);
$value1 = ($_POST['value1']) ?? null;
$value2 = $_POST['value2'] ?? null;
$value3 = $_POST['value3'] ?? null;
}
}
// insert size
$sql = "INSERT INTO valueSize(value1, value2, value3) values('$value1', '$value2, '$value3')";
$stmt = $pdo->prepare($sql);
$stmt->execute();
?>
<!DOCTYPE html>
<html lang="en">
<form action="">
<?php foreach ($categoriesTypeId as $key): ?>
<div class="dropItem invisible" id="<?php echo $key['id'] ?>">
<label for="value1"><?php echo $key['value1Type'] ?>:</label>
<input type="text" name="value1[]" value="<?php echo $value1 ?>">
<div class="error"><?php echo $error['value1'] ?></div>
<?php if (isset($key['value2Type'])) {?>
<div class="inputCheck">
<label for="value2"><?php echo $key['value2Type'] ?>
:</label>
<input type="text" name="value2" value="<?php echo $value2 ?>">
<div class="error"><?php echo $error['value2'] ?></div>
</div>
<?php
}
;
?>
<?php if (isset($key['value3Type'])) {?>
<div class="inputCheck">
<label for="value3"><?php echo $key['value3Type'] ?>
:</label>
<input type="text" name="value3" value="<?php echo $value3 ?>">
<div class="error"><?php echo $error['value3'] ?></div>
</div>
<?php
} else {
}
?>
<?php endforeach;?>
</form>
</html>

How to get information from an already populated form

I am trying to update a database record using a submit button, however the form that information is being taken from has itself been populated by another submit button.
The PHP is below:
if (isset($_POST['edit']))
{
$editUser = User::locate($_POST['userSelect']);
$setCompany = Company::findCompany();
$exCompanyId = $editUser->user_company_id;
$isAdmin = $editUser->user_admin;
$eUserFirstname = $editUser->user_firstname;
$eUserLastname = $editUser->user_lastname;
$eUserUsername = $editUser->user_username;
$eUserEmail = $editUser->user_email;
$eUserCompany = $editUser->user_company;
$adminArray = array("Yes","No");
if ($exCompanyId > NULL)
{
$exCompany = Company::locateId($exCompanyId);
$companyValue = $exCompany->company_name;
}
else
{
$companyValue = "";
}
if ($isAdmin > 0)
{
$userAdmin = $adminArray[0];
}
else
{
$userAdmin = $adminArray[1];
}
}
if (isset($_POST['confirm']))
{
$updateUserRecord = User::locate($eUserUsername);
$newCompanyValue = $_POST['setCompany'];
$isAdmin = $_POST['userAdmin'];
$newCompany = Company::locate($newCompanyValue);
$companyId = $newCompany->company_id;
$updateUserRecord->user_company_id = $companyId;
$updateUserRecord->user_admin = $isAdmin;
$editUser->updateUserAdmin();
$message = "You have successfully updated the user account";
echo "<script type='text/javascript'>alert('$message');</script>";
}
The HTML code is below:
<form action="" method="post">
<p>
<label>First Name:</label>
<label><?php echo $eUserFirstname ?></label>
</p>
<p>
<label>Last Name:</label>
<label><?php echo $eUserLastname ?></label>
</p>
<p>
<label>Username:</label>
<label><?php echo $eUserUsername ?></label>
</p>
<p>
<label>Email Address:</label>
<label><?php echo $eUserEmail ?></label>
</p>
<p>
<label>User Company:</label>
<label><?php echo $eUserCompany ?></label>
</p>
<p>
<label>Set Company:</label>
<select name="setCompany">
<option><?php echo $companyValue ?></option>
<?php
foreach ($setCompany as $srow)
{
?>
<option id="<?=
$srow->company_id
?>">
<?=
$srow->company_name
?>
</option>
<?php
}
?>
</select>
</p>
<p>
<label>Administrator:</label>
<select name="userAdmin">
<option><?php echo $userAdmin ?></option>
<?php
foreach ($adminArray as $arow)
{
?>
<option>
<?=
$arow
?>
</option>
<?php
}
?>
</select>
</p>
<input type="submit" name="cancel" value="Cancel">
<input type="submit" name="confirm" value="Confirm">
</br>
</form>
From my investigations so far the variables aren't transferring to the 2nd if statement, and I'm not sure how to make them available to it.
Store the first form's submission in hidden input fields alongside your echo statements. For example: <input type="hidden" name="field_name" value="<?php echo htmlspecialchars($field); ?>">
Or, alternatively, update the DB after first form posts.

PHP mysqli while loop called twice

I have this code:
$q = 'SELECT * FROM languages ORDER BY id DESC';
$r = mysqli_query($dbc, $q);
while($langs = mysqli_fetch_assoc($r)){
$l_id = $langs['id'];
$l_name = $langs['name_en'];
?>
<li <?php if($l_id == '1'){ echo 'class="active"'; }?>><?php echo $l_name;?></li>
<?php } // closing 1st while loop
?>
</ul>
<div class="tab-content">
<?php
$q = 'SELECT * FROM languages ORDER BY id DESC';
$r = mysqli_query($dbc, $q);
while($langs = mysqli_fetch_assoc($r)){
$l_id = $langs['id'];
$l_lang = $langs['language'];
$l_name = $langs['name_en'];
?>
<div class="tab-pane fade <?php if($l_id == '1'){ echo 'in active'; }?>" id="<?php echo $l_name;?>">
<div class="form-group col-xs-4">
<label for="title_<?php echo $l_lang;?>">Title:</label>
<input class="form-control" type="text" name="title_<?php echo $l_lang;?>" id="title_<?php echo $l_lang;?>" value="<?php echo $opened['title_'.$l_lang.'']; ?>" placeholder="Page Title">
</div>
<div class="form-group col-xs-4">
<label for="header_<?php echo $l_lang;?>">Header:</label>
<input class="form-control" type="text" name="header_<?php echo $l_lang;?>" id="header_<?php echo $l_lang;?>" value="<?php echo $opened['header_'.$l_lang.'']; ?>" placeholder="Page Header">
</div>
<div class="form-group col-xs-<?php if($page_type == 'tour'){ echo 8;}else {echo 12;} ?>">
<label for="body_<?php echo $l_lang;?>">Body:</label>
<textarea class="form-control editor" name="body_<?php echo $l_lang;?>" id="body_<?php echo $l_lang;?>" rows="7" placeholder="Page Body"><?php echo $opened['body_'.$l_lang.'']; ?></textarea>
</div>
</div>
<?php } //closing 2nd while loop
?>
</div>
When running it, the result is a tabbed form (I skipped the form tags and some html from above, to reduce the code writen) and everything is OK.
My questions are:
How to have the same output, but with a single query and while loop?
Is it possible to make this a function? Any hints?
Thank you!
I think you will have to loop twice, but you don't need to make two queries at all!
Use mysqli_fetch_all to store the results in an array and then loop through it
For example:
$q = 'SELECT * FROM languages ORDER BY id DESC';
$r = mysqli_query($dbc, $q);
$langs = mysqli_fetch_all($r);
foreach($langs as $lang){
//render links
}
//...
foreach($langs as $lang){
//render tabs
}
Your script will run much faster
Try
<?php
function get_rows()
{
$q = 'SELECT * FROM languages ORDER BY id DESC';
$r = mysqli_query($dbc, $q);
$languages = array();
while($langs = mysqli_fetch_assoc($r))
{
$languages[] = $langs;
}
return $languages;
}
$languages = get_rows();
if($languages != false)
{
?>
<ul>
<?php
foreach($languages as $langs)
{
$l_id = $langs['id'];
$l_name = $langs['name_en'];
?>
<li <?php
if($l_id == '1')
{
echo 'class="active"';
}
?>><?php echo $l_name; ?></li>
<?php
}
?>
</ul>
<div class="tab-content">
<?php
foreach($languages as $langs)
{
$l_id = $langs['id'];
$l_lang = $langs['language'];
$l_name = $langs['name_en'];
?>
<div class="tab-pane fade <?php
if($l_id == '1')
{
echo 'in active';
}
?>" id="<?php echo $l_name; ?>">
<div class="form-group col-xs-4">
<label for="title_<?php echo $l_lang; ?>">Title:</label>
<input class="form-control" type="text" name="title_<?php echo $l_lang; ?>" id="title_<?php echo $l_lang; ?>" value="<?php echo $opened['title_' . $l_lang . '']; ?>" placeholder="Page Title">
</div>
<div class="form-group col-xs-4">
<label for="header_<?php echo $l_lang; ?>">Header:</label>
<input class="form-control" type="text" name="header_<?php echo $l_lang; ?>" id="header_<?php echo $l_lang; ?>" value="<?php echo $opened['header_' . $l_lang . '']; ?>" placeholder="Page Header">
</div>
<div class="form-group col-xs-<?php
if($page_type == 'tour')
{
echo 8;
}
else
{
echo 12;
}
?>">
<label for="body_<?php echo $l_lang; ?>">Body:</label>
<textarea class="form-control editor" name="body_<?php echo $l_lang; ?>" id="body_<?php echo $l_lang; ?>" rows="7" placeholder="Page Body"><?php echo $opened['body_' . $l_lang . '']; ?></textarea>
</div>
</div>
<?php } ?>
</div>
<?php
}
?>
Save results while first iterating in some array and the iterate over this array for the second time:
$tmp_array = array();
while ($langs = mysqli_fetch_assoc($r)){
$tmp_array[] = $langs;
}
//some code here
//and second array
foreach ($tmp_array as $langs) {
//more code here
}

i want to strore multiple values in database but storing only one value

<div class="row" id="related_campaigns">
<div class="span6">
<div class="control-group">
<label class="control-label" for="inputgaveabonnement">
Campaign<b>*</b>
</label>
<div class="controls">
<div class="fieldrow_horz1">
<div class="fieldgroup">
<label>
Name <b>*</b>
</label>
</div>
<div class="fieldgroup">
<label>
Image <b>*</b>
</label>
</div>
<div class="fieldgroup">
<label>
Url <b>*</b>
</label>
</div>
</div>
</div>
<?php
// Save to array so that we can loop it out later =)
$result = db2_getrelcamp( array('campaigns_id' => $this_id) );
if (!is_null($result))
{
$numsub1 = 0;
while ( $row = $result->fetch_object() )
{
$numsub1 = $numsub1 +1;
array_push($arrcampaignname, $row->campaignname);
array_push($arrcampaignimage, $row->campaignimage);
array_push($arrcampaignurl, $row->campaignurl);
}
$formcountfld1=$numsub1;
}
else
{
$numsub1=1;
$formcountfld1=1;
}
$i = 1;
if (1==1) {
for ($i = 1; $i <= $numsub1; $i++) {
?>
<div class="fieldrow_horz1">
<div class="fieldgroup">
<input type="text" id="campaignname_<?= $i ?>" name="campaignname_<?= $i ?>" value="<?= isset($arrcampaignname[$i-1]) ? $arrcampaignname[$i-1] : '' ?>" />
</div>
<div class="fieldgroup">
<select id="campaignimage_<?= $i ?>" name="campaignimage_<?= $i ?>" alue="<?= isset($arrcampaignimage[$i-1]) ? $arrcampaignimage[$i-1] : '' ?>" />
<?php
// * Hæmta alla filer i mappen
$dir = "../images/campaigns/";
$files = scandir($dir);
$strSelected = "";
$somethingChecked = false;
foreach($files as $key => $value)
{
if ($value != '.' && $value != '..')
{
if ( $formImage === $value ) {
$strSelected = ' selected="selected"';
$somethingChecked = true;
} else
$strSelected = '';
echo '<option value="' . $value . '"' . $strSelected . '>' . $value . '</option>';
}
}
?>
<?php
if ($somethingChecked) {
$strSelected = '';
} else {
$strSelected = ' selected="selected"';
}
echo "<option disabled='disabled'></option>";
echo '<option value=""' . $strSelected .'>- Ikke bruk bilde -</option>';
?>
</select>
<p class="help-block">A Related image for this campaign from your image archive (upload).</p>
</div>
<div class="fieldgroup">
<input type="text" id="campaignurl_<?= $i ?>" name="campaignurl_<?= $i ?>" value="<?= isset($arrcampaignurl[$i-1]) ? $arrcampaignurl[$i-1] : '' ?>" />
</div>
<img src="../images/remove.png" alt="Delete" class="link" id="remove_<?= $i ?>" />
</div>
<?php /*?>value="<?= isset($arrEtternavn[$i-1]) ? $arrEtternavn[$i-1] : '' ?>"
<?php value="<?= $arrEtternavn[$i-1] ?>" */?> <?php
}
} else {
?>
<div class="fieldrow_horz1" >
<div class="fieldgroup">
<input type="text" id="campaignname_<?= $i ?>" name="campaignname_<?= $i ?>" value="" />
</div>
<div class="fieldgroup">
<select id="campaignimage_<?= $i ?>" name="campaignimage_<?= $i ?>" class="span3" >
<?php
// * Hæmta alla filer i mappen
$dir = "../images/campaigns/";
$files = scandir($dir);
$strSelected = "";
$somethingChecked = false;
foreach($files as $key => $value)
{
if ($value != '.' && $value != '..')
{
if ( $formImage === $value ) {
$strSelected = ' selected="selected"';
$somethingChecked = true;
} else
$strSelected = '';
echo '<option value="' . $value . '"' . $strSelected . '>' . $value . '</option>';
}
}
?>
<?php
if ($somethingChecked) {
$strSelected = '';
} else {
$strSelected = ' selected="selected"';
}
echo "<option disabled='disabled'></option>";
echo '<option value=""' . $strSelected .'>- Ikke bruk bilde -</option>';
?>
</select>
<p class="help-block">A Realated image for this campaign from your image archive (upload).</p>
</div>
<div class="fieldgroup">
<input type="text" id="campaignurl_<?= $i ?>" name="campaignurl_<?= $i ?>" value="" />
</div>
</div>
<?php
}
?>
<?php
$thisId = "countfld1";
$thisVar = $formcountfld1; ?>
<input type="hidden" name="<?= strtolower($thisId) ?>" class="input-medium" id="input<?= $thisId ?>" value="<?= htmlspecialchars($thisVar, ENT_QUOTES) ?>" />
<p class="leggtil1">
+ Legg til flere
</p>
</div>
</div>
</div>
i want to store multiple aticle values in database like title ,image and url but itis storing only one value in db any suggestion .thanks in advance
Look at the documentation for
serialize();
unserialize();
They basically encode and decode, an array or object into a specific text based encoded string, a little like JSON, but its not JSON.
They are used all over, look at a wordpress database for example.
serialize()
unserialize()

Categories