Here is my situation:
I am building a multi language page where my language come out of MYSQL through a PDO statement. The language is selected through an ID inside my link.
For Example:
<div class="country"><a class="darkbrowntext" href="log?id=11111">
<div class="language"><p>Nederlands</p></div>
<div class="countryflag"><img src="images/Dutch.gif" width="220" height="145" alt="dutch"></div>
</a></div>
<div class="country"><a class="darkbrowntext" href="log?id=11112">
<div class="language"><p>English</p></div>
<div class="countryflag"><img src="images/english.gif" width="220" height="145" alt="french"></div>
</a></div>
On my log page i have the following code:
$languageid = (int) $_GET['id'];
if(isset($languageid)){
$_SESSION['languageid'] = $languageid;
}
else {
header('Location: error');
}
require('incl/connect.inc.php');
if(isset($languageid) && ($languageid == 11111)){
$column = 'language_nl';
}
elseif(isset($languageid) && ($languageid == 11112)){
$column = 'language_en';
}
$languagestmt = $db->prepare ("SELECT $column FROM attrib_language");
$languagestmt->execute();
$lrow = $languagestmt->fetch(PDO::FETCH_ASSOC);
If a have this table in my MYSQL:
ID language_nl language_en
1 Aanmelden Sign in
2 Afmelden Sign out
3 Naam Name
4 Voornaam First name
How will I be able to select the right thing in my array of my column to put it into the right labels see below. Do I have to work with foreach or do I have to change my fetch?
<div class="logtxt"><h3><?php echo $lrow['***********']; ?></h3></div>
<div class="logtxt"><h3><?php echo $lrow['***********']; ?></h3></div>
<label for="name" class="labelname"><?php echo $lrow['***********']; ?></label><input type="text" name="name" id="name" class="inputname" onclick="fncCheck();"/>
<label for="firstname" class="labelfirstname"><?php echo $lrow['***********']; ?></label><input type="text" name="firstname" id="firstname" class="inputfirstname" onclick="fncCheck();"/>
$languagestmt = $db->prepare ("SELECT ID,$column FROM attrib_language");
$lrow = $languagestmt->fetchAll(PDO::FETCH_COLUMN || PDO::FETCH_GROUP, 0);
<?php echo $lrow[1]; ?>
or
$languagestmt = $db->prepare ("SELECT language_en,$column FROM attrib_language");
$lrow = $languagestmt->fetchAll(PDO::FETCH_COLUMN || PDO::FETCH_GROUP, 0);
<?php echo $lrow['Sign In']; ?>
Whichever you prefer.
$res = $db->prepare ("SELECT a,b,c FROM my_table");
$res->execute();
while($row = $res->fetch(PDO::FETCH_ASSOC){
echo $row['a'] . $row['b'],$row['c'];
}
Notice the $row is an associative array where the members are the column names.
I suggest you print_r($lrow); or var_dump($lrow); to see your results in what shape or form. Also to have a cleaner view, echo "<pre>"; before it.
PDO::FETCH_ASSOC: returns an array indexed by column name as returned in your result set
So it should be:
<div class="logtxt"><h3><?php echo $lrow[$column][1]; ?></h3></div>
<div class="logtxt"><h3><?php echo $lrow[$column][2]; ?></h3></div>
<label for="name" class="labelname"><?php echo $lrow[$column][3]; ?></label><input type="text" name="name" id="name" class="inputname" onclick="fncCheck();"/>
<label for="firstname" class="labelfirstname"><?php echo $lrow[$column][4]; ?></label><input type="text" name="firstname" id="firstname" class="inputfirstname" onclick="fncCheck();"/>
However, if I were you I would store each of the translations in a different file (utf-8 encoded) instead of a database.
Related
I need to call out a the other data from the database and display it through modal but i cant call out the ID from my html table.
Here's my html table and php for posting some of the data's :
<?php
$connection = mysql_connect('localhost', 'root', ''); //The Blank string is the password
mysql_select_db('ts_php');
$query = "SELECT * FROM job_posted"; //You don't need a ; like you do in SQL
$result = mysql_query($query);
echo "<table class='table'>
<thead>
<th>JOB</th>
<th>STATUS</th>
<th>APPLICATIONS</th>
<th>EDIT</th>
<th>DELETE</th>
</thead>
"; // start a table tag in the HTML
while($row = mysql_fetch_array($result)){ //Creates a loop to loop through results
echo "<tr>
<th>" . $row['job_title'] . "</th>
<td>" . $row['status'] . "</td>
<td>" . $row['applications'] . "</td>
<td><a class='openModal' data-id='".$row['post_ID']."'>edit</a></td>
<td><a href='#'>delete</a></td>
</tr>";
}
echo "</table>"; //Close the table in HTML
?>
And here is my php inside the modal body
<?php
(LINE 121:) $queryEdit = "SELECT * FROM job_posted WHERE post_ID = '$row['post_ID']' ";
$resultEdit = mysql_query($queryEdit, $connection);
while($row1 = mysql_fetch_array($resultEdit)){
?>
<span>Name:</span> <?php echo $row1['job_title']; ?><br>
<span>loc:</span> <?php echo $row1['location']; ?><br>
<span>job type:</span> <?php echo $row1['job_type']; ?><br>
<span>description:</span> <?php echo $row1['job_desc']; ?><br>
<?php
}
?>
Here is the Execution error
Notice: Array to string conversion in C:\xampp\htdocs\talent_space_php\employer\jobs_posted.php on line 121
PS. I just knew recently that the mysql_* will be depreciated, so im planning to finish this first and then convert into MYSQLI or PDO.
I did something similar recently.
The way i worked around it was this:
While generating my rows i added data properties to my modal toggler (in my case a button). Just like you added data-id. However i did this for every value that i need.
I simply used a jquery function to add my data into input fields inside of my modal.
Button table row generation:
<td><a href="#" class="editId" data-hidden="' . $product['id'] . '" data-name="' . $product['name'] .'"
data-short="' . $product['kuerzel'] . '"data-anr="' . $product['anumber'] . '"
data-comment="' . $product['comment'] . '" data-toggle="modal"
data-target="#editProductModal"><i class="far fa-edit"></i></a></td>
My Modal:
<div class="modal-body">
<input type="hidden" name="action2" value="edit">
<div class="form-row">
<div class="col-4">
<div class="form-group">
<input type="hidden" class="form-control" name="hidden" id="modal_hidden" value="">
</div>
<div class="form-group">
<label for="select_p" class="col-form label">Produkt:</label>
<input type="text" class="form-control" name="select" id="select_p" value="" required disabled>
</div>
<div class="form-group">
<label for="new_products_add_name" class="col-form label">New Name:</label>
<input type="text" class="form-control" name="new_name" id="new_products_add_name">
</div>
<div class="form-group">
<label for="new_products_add_short" class="col-form label">New Short:</label>
<input type="text" class="form-control" name="new_short" id="new_products_add_short">
</div>
<div class="form-group">
<label for="new_products_add_number" class="col-form label">New ProductNr:</label>
<input type="text" class="form-control" name="new_number" pattern="{3,11}" id="new_products_add_number">
</div>
<div class="form-group">
<label for="new_products_add_comment" class="col-form label">New Comment:</label>
<input type="text" class="form-control" name="new_comment" id="new_products_add_comment">
</div>
</div>
</div>
</div>
Function:
<script>
$(document).on("click",".editId",function() {
var hidden_value = $(this).data('hidden');
var name_value = $(this).data('name');
var short_value = $(this).data('short');
var anr_value = $(this).data('anr');
var comment_value = $(this).data('comment');
$('#modal_hidden').val(hidden_value);
$('#select_p').val(name_value);
$('#new_products_add_name').val(name_value);
$('#new_products_add_short').val(short_value);
$('#new_products_add_number').val(anr_value);
$('#new_products_add_comment').val(comment_value);
});
</script>
I hope this helps. This might not be the cleanest way of doing this, but it worked for me (and my table contains a fairly large number of rows).
First, I suggest you to not echo out html values.
Second, I would use pdo there are enough examples on the internet. A few tutorials should help you out.
As of your problem:
Change this:
$queryEdit = "SELECT * FROM job_posted WHERE post_ID = '$row['post_ID']' ";
Into one of these:
$queryEdit = "SELECT * FROM job_posted WHERE post_ID = " + $row['post_ID'];
$queryEdit = "SELECT * FROM job_posted WHERE post_ID = {$row['post_ID']} ";
I'm trying to do an update page, the data that I'm fetching are input texts, dates, dropdowns and a textarea.
The data on input texts & date works completely fine however, the data on my dropdowns and textarea wont appear.
I still can update it, the only problem here is it just won't appear. So I'm hoping if anybody can help me and see what I missed in my coding.
How my form looks like:
Solution:
I've found the solution to this question and below is my new html coding. There a few php coding that I edited at the dropdown and the dates.
Updated html code:
php code that are outside from my html:
<?php
session_start();
require('db.php');
include("auth.php");
$noID=$_REQUEST['noID'];
$query = "SELECT * from daftartempah where noID='".$noID."'";
$result = mysqli_query($con, $query) or die ( mysqli_error());
$row = mysqli_fetch_assoc($result);
?>
php code that are inside my html:
<?php
$status = "";
if(isset($_POST['new']) && $_POST['new']==1)
{
$noID=$_REQUEST['noID'];
$trn_date = date("Y-m-d H:i:s");
$pemohon =$_REQUEST['pemohon'];
$trkhMula = $_REQUEST['trkhMula'];
$trkhAkhir =$_REQUEST['trkhAkhir'];
$n_program = $_REQUEST['n_program'];
$lokasi =$_REQUEST['lokasi'];
$n_anjuran = $_REQUEST['n_anjuran'];
$catatan = $_REQUEST['catatan'];
$status_hr = $_REQUEST['status_hr'];
$submittedby = $_SESSION["username"];
$update="update daftartempah set trn_date='".$trn_date."', pemohon='".$pemohon."', trkhMula='".$trkhMula."', trkhAkhir='".$trkhAkhir."', n_program='".$n_program."', lokasi='".$lokasi."', n_anjuran='".$n_anjuran."', catatan='".$catatan."', status_hr='".$status_hr."', submittedby='".$submittedby."' where noID='".$noID."'";
mysqli_query($con, $update) or die(mysqli_error());
$status = "Record Updated Successfully. </br></br>
<a href='Page8.php'>View Updated Record</a>";
echo '<p style="color:#FF0000;">'.$status.'</p>';
}else {
?>
Dropdowns:
<div class="form-group row text-left">
<label for="example-text-input" class="col-3 col-form-label"><b>Dept/Kelab/Anjuran: </b></label>
<div class="col-8">
<select class="form-control" name="n_anjuran" id="namaAnjuran">
<option selected name="" value="<?php echo $row['n_anjuran'];?>"><?php echo $row['n_anjuran'];?></option>
<?php
$sql = mysqli_query($con, "SELECT kd_dept, desc_dept From koddept");
$rows = mysqli_num_rows($sql);
while ($rows = mysqli_fetch_array($sql)){
echo "<option value='". $rows['kd_dept'] ."'>" .$rows['desc_dept'] ."</option>" ;
}
?>
</select>
</div>
</div>
Textarea:
<div class="form-group row text-left">
<label for="exampleTextarea" class="col-3 col-form-label"><b>Catatan: </b></label>
<div class="col-8">
<textarea class="form-control" rows="3" name="catatan" required><?php echo $row['catatan'];?></textarea>
</div>
</div>
My new form:
All questions are welcome, feel free to comment and ask, I'll try to help.
This is my first post in this forum, despite being a devoted follower for years now.
I have built a simple system that registers lot numbers and their locations within a MySQL database through a PHP form.
Then i have this other form called "Errata Corrige" that I use to find and edit eventual mistaken entries.
It's search criteria is an (UNSIGNED INT UNIQUE) value named "lotto" and everything works (worked) like a charm under this circumstances.
Now the thing got a little tricky.
I found out that lot numbers (lotto) for work purposes are not always unique values, there might be more than one entry with the same number.
No problem making the "Insert" form or various counters work under this new circumstances, but it got really tricky within the EDIT functions.
This is my PHP code: `
<?php
$id = "";
$settore = "";
$ubicazione = "";
$numero = "";
$lotto="";
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
// connect to mysql database
try{
$connect = mysqli_connect($host, $user, $password, $database);
} catch (mysqli_sql_exception $ex) {
echo 'Error';
}
// get values from the form
function getPosts()
{
$posts = array();
$posts[0] = $_POST['id'];
$posts[1] = $_POST['settore'];
$posts[2] = $_POST['ubicazione'];
$posts[3] = $_POST['numero'];
$posts[4] = $_POST['lotto'];
return $posts;
}
// Search
if(isset($_POST['search']))
{
$data = getPosts();
$search_Query = "SELECT * FROM mappa WHERE lotto = $data[4]";
$search_Result = mysqli_query($connect, $search_Query);
if($search_Result)
{
if(mysqli_num_rows($search_Result))
{
while($row = mysqli_fetch_array($search_Result))
{
$id = $row['id'];
$settore = $row['settore'];
$ubicazione = $row['ubicazione'];
$numero = $row['numero'];
$lotto = $row ['lotto'];
}
}else{
echo 'Lotto non presente in archivio';
}
}else{
echo 'Error';
}
}
// Insert
if(isset($_POST['insert']))
{
$data = getPosts();
$insert_Query = "INSERT INTO `mappa`(`settore`, `ubicazione`, `numero`, `lotto` ) VALUES ('$data[1]','$data[2]',$data[3], $data[4])";
try{
$insert_Result = mysqli_query($connect, $insert_Query);
if($insert_Result)
{
if(mysqli_affected_rows($connect) > 0)
{
$resInsert = "1 nuovo dato inserito correttamente!";
}else{
$resInsert = "Nessun dato inserito";
}
}
} catch (Exception $ex) {
echo 'Errore '.$ex->getMessage();
}
}
// Edit
if(isset($_POST['update']))
{
$data = getPosts();
$update_Query = "UPDATE `mappa` SET `settore`='$data[1]',`ubicazione`='$data[2]',`numero`=$data[3],`lotto`=$data[4] WHERE `id` = $data[0]";
try{
$update_Result = mysqli_query($connect, $update_Query);
if($update_Result)
{
if(mysqli_affected_rows($connect) > 0)
{
$resAgg = "1 dato aggiornato correttamente!";
}else{
$resAgg = "Nessun dato aggiornato!";
}
}
} catch (Exception $ex) {
echo 'Error Update '.$ex->getMessage();
}
} ?>
`
HTML:
<form action="mod.php" method="post" class="form-horizontal form-bordered" style="text-align:center">
<div class="form-group has-error" style="padding-top:30px">
<label class="col-xs-3 control-label" for="state-normal">ID</label>
<div class="col-lg-3">
<input type="text" name="id" placeholder="ID" class="form-control" value="<?php echo $id;?>"> </div>
</div>
<div class="form-group">
<label class="col-md-3 control-label" for="state-normal">Settore</label>
<div class="col-md-6">
<input type="text" name="settore" placeholder="Settore" class="form-control" value="<?php echo $settore;?>"> </div>
</div>
<div class="form-group">
<label class="col-md-3 control-label" for="state-normal">Ubicazione</label>
<div class="col-md-6">
<input type="text" name="ubicazione" placeholder="Ubicazione" class="form-control" value="<?php echo $ubicazione;?>"> </div>
</div>
<div class="form-group">
<label class="col-md-3 control-label" for="state-normal">Numero</label>
<div class="col-md-6">
<input type="text" name="numero" placeholder="Numero" class="form-control" value="<?php echo $numero;?>"> </div>
</div>
<div class="form-group has-success">
<label class="col-md-3 control-label" for="state-normal">Lotto</label>
<div class="col-md-6">
<input type="text" name="lotto" placeholder="Lotto" class="form-control" value="<?php echo $lotto;?>"> </div>
</div>
<div style="padding-top:16px">
<!-- Insert-->
<button type="submit" name="insert" value="Add" class="btn btn-effect-ripple btn-primary">Inserisci</button>
<!-- Update-->
<button type="submit" name="update" value="Update" class="btn btn-effect-ripple btn-info">Aggiorna</button>
<a> </a>
<!-- Search-->
<button type="submit" name="search" value="Find" class="btn btn-effect-ripple btn-success">Cerca</button>
</div>
</form>
While the lot number was unique everything worked like a charm.
Now that there are multiple data with the same lot number the code became obsolete since the "search" function only shows the last (greatest ID) data.
I have tried to work around a loop and tell the function to search every ID where lotto = lotto but it didn't work.
A simple solution would be obviously searching through ID instead of lotto but that is a pretty crapy one, since the user only knows (and is interested in) Lot Numbers not the ID it was assigned during data insertion.
Then I tried to put two php functions into one page, the first that fetches data from Mysql into a PHP dropdown menu, telling it to show every ID that matches the search criteria (lotto):
<?php if (isset($_POST['submitted'])){
include ('../mysql_connect.php'); // connessione al database
$category = 'lotto';
$criteria = $_POST['criteria'];
$query = "SELECT * FROM mappa WHERE $category = '$criteria'";
$result = mysqli_query($dbcon, $query) or die('Impossibile reperire i dati');
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$idTab = $row['id'];
echo "<option>
$idTab </option>";
}
} // FINE if ?>
</select>
Fetching data from MySQL into the dropdown worked just fine, but I got stucked in the syntax trying to use this dropdown as a search criteria for my first function.
Every help would really be appreciated! Thank you in advance for your answers.
You said that lotto is unique. So how come you are able to insert multiple rows with the same lotto?
Remove the unique constraint from the lotto column.
Try the following:
$query = select lotto, group_concat(id) as ID numbers from mappa where lotto = 'user search number' group by lotto;
$result = $conn->query($query);
$rows = $result->num_rows;
$result->data_seek(0); //move to first row (which is the only one)
$row = $result->fetch_array(MYSQLI_NUM); //fetch array
$id_numbers_string = $row[1]; //store the values of the row's second column (which is number 1)
$id_numbers_separated_array = explode(",", $id_numbers_string); //create an array with the values in the string
for($i = 0; $i < count($id_numbers_separated_array); $i++){ //loop through created array
echo "ID: " . $id_numbers_separated_array[$i];
echo "<br>";
}
Also try to run the query in your database management system to see the results.
So I've got a form to edit entries which is populating with what has already been entered from the database. When I make an edit, it is saving and redirecting me back to the listing page with no errors, but it's not changing anything. I'm guessing it's getting confused as to where to pull the values from.
This is the the SQL Query to populate the form with the values (this part works):
<?php
// query db
$gigid = $_GET['gigid'];
$con = mysqli_connect("***********","***********","***********","***********");
$result = mysqli_query($con, "SELECT * FROM gigs WHERE gigid=$gigid") or die(mysqli_error());
$row = mysqli_fetch_array($result);
mysqli_close($con);
// check that the 'id' matches up with a row in the databse
if($row)
{
// get data from db
$gig_name = $row['gig_name'];
$gig_type = $row['gig_type'];
$gig_date = $row['gig_date'];
$gig_customer = $row['gig_customer'];
$gig_venue = $row['venue_name'];
$gig_fee = $row['gig_fee'];
$gig_status = $row['gig_status'];
}
?>
This is an excerpt from the form:
<form class="form-horizontal" id="create-ticket" method='post' action='edit_gig_process.php? gigid=<?php echo $_GET['gigid']; ?>'>
<fieldset>
<legend>Edit Gig Information</legend>
<input type="hidden" class="input-xxlarge" id="gig_date_created" name="gig_date_created">
<input type="hidden" class="input-xxlarge" id="userid" name="userid">
<div class="control-group">
<label class="control-label" for="gigid">Gig ID</label>
<div class="controls">
<input type="text" name="gigid" disabled="disabled" value="<?php echo $_GET['gigid']; ?>" />
</div>
</div>
<div class="control-group">
<label class="control-label" for="gig_name">Gig Name (Required)</label>
<div class="controls">
<input type="text" class="input-xxlarge" id="gig_name" value="<?php echo $row['gig_name']; ?>" name="gig_name">
</div>
</div>
This is an excerpt from the update query:
$gigid = $_GET['gigid'];
$sql= "UPDATE gigs set
gig_name='$gig_name',
gig_type='$gig_type',
gig_customer='$gig_customer',
gig_date='$gig_date_created',
gig_start_time='$gig_start_time',
gig_end_time='$gig_end_time',
gig_fee='$gig_fee',
gig_status='$gig_status',
venue_name='$venue_name',
venue_address='$venue_address',
venue_contact='$venue_contact',
WHERE
gigid='$gigid'";
header('Location: http://managegigs.com/cp/my-gigs.php');
mysqli_close($con);
You are not running a update query, your update is just a string.
After
$sql= "UPDATE gigs set
gig_name='$gig_name',
gig_type='$gig_type',
gig_customer='$gig_customer',
gig_date='$gig_date_created',
gig_start_time='$gig_start_time',
gig_end_time='$gig_end_time',
gig_fee='$gig_fee',
gig_status='$gig_status',
venue_name='$venue_name',
venue_address='$venue_address',
venue_contact='$venue_contact'
WHERE
gigid='$gigid'";
add:
mysqli_query($con,$sql);
also, at least change this:
$gigid = $_GET['gigid'];
add:
$gigid = mysqli_real_escape_string($gigid);
directly after to have it a little secured. Putting $_GET directly to DB is dangerous.
Uploadpart in complete:
$gig_name = $_POST['gig_name'];
// fetch all $_POST(ed) data
// and secure with
$gig_name = mysqli_real_escape_string($con,$gig_name);
$gigid = $_GET['gigid'];
$gigid = mysqli_real_escape_string($con,$gigid);
$sql= "UPDATE gigs set
gig_name='$gig_name',
gig_type='$gig_type',
gig_customer='$gig_customer',
gig_date='$gig_date_created',
gig_start_time='$gig_start_time',
gig_end_time='$gig_end_time',
gig_fee='$gig_fee',
gig_status='$gig_status',
venue_name='$venue_name',
venue_address='$venue_address',
venue_contact='$venue_contact'
WHERE
gigid='$gigid'";
mysqli_query($con,$sql);
header('Location: http://managegigs.com/cp/my-gigs.php');
mysqli_close($con);
$query = "SELECT * FROM abc";
if ($result = $db->query($query)) {
$row = $result->fetch_assoc();
while ($row = $result->fetch_assoc()) {
$data[] = array("a"=>$row["a"],
"b"=>$row["b"],
"c"=>$row["c"],
"d"=>$row["d"]
);
}
$result->close();
}
$db->close();
<?php foreach ($data as $row) { ?>
<form action="">
<div class="col-lg-3">
<input type="text" value="<?php echo $row['a'] ?>"/>
</div>
<div class="col-lg-3">
<input type="text" value="<?php echo $row['b'] ?>"/>
</div>
<div class="col-lg-3">
<input type="text" value="<?php echo $row['c'] ?>"/>
</div>
<div class="col-lg-3">
<input type="text" value="<?php echo $row['d'] ?>"/>
</div>
</form>
<?php } ?>
Above is the exact code that I used to retrieve all the row in a table. The strange part is that I have 5 row and it only show 4. The first row is missing. I wonder why.
In your code, you're calling $row = $result->fetch_assoc(); just before starting your while loop. This line is "consuming" your first raw and when you're entering in the loop, you move the cursor to the second row by calling this same method a second time.
The condition of the while loop is executed BEFORE the content, here you'll find more info about PHP While loop
To fix your code, remove this first line.