my table fields are as below:
name | email | status | img_1 | img_2 | img_3 | img_4 | img_5 | img_6 | avail
so i want to add 6 images via a loop using php. but i cannot write the query to add images which are not static(i mean to say if some one add only 2 images or may be 1 images at the entry). pls help me. this is my basic query which i thought work.
$sql = "update product set img_".$dbfield." ='".$filename1."' where
id='".$iiid."'";
where $dbfield is the increment one by one to match the field name in img_1 img_2.
thank yoou in advance.
I m not sure what are achieving but I m assuming you want some think this.
$query = "UPDATE product SET";
$comma = " ";
$whitelist = array(
'img_1',
'img_2',
'img_3',
'img_4',
'img_5',
'img_6',
// ...etc
);
foreach($_POST as $key => $val) {
if( ! empty($val) && in_array($key, $whitelist)) {
$query .= $comma . $key . " = '" . mysql_real_escape_string(trim($val)) . "'";
$comma = ", ";
}
}
$sql = mysql_query($query);
Related
i don't know how to explain it so i will show you what i am trying to do.
mysql tables
Table Rules:
id| name|maker|model |type|price_rule
1 |client1|bmw |x5 | |24
2 |client2| | |2.0 |13
Table Products_client1:
id|maker|model |type|price
1 |bmw |x5 |2.4 |10
2 |opel |vectra|1.6 |5
3 |ford |mondeo|2.0 |15
now, i have a list of over 500k products in my mysql db, and need to update the price in the products table, with the rules from the rules table :
UPDATE products_client1,client2,... SET price=price+price_rule WHERE maker=maker or model=model or type=type
the thing is that not all clients in the Rule table have maker and model and type, some have only maker or only model or only type or a combination of the 3 and every client has a separate products table.
products_client1, products_client2.....
$sql = "SELECT * FROM Table_rule";
$Res = $conn->query ($sql) or die ("Error : " . $sql . "<br>" . $conn->error);
While($Row = $Res->fetch_assoc ()) { if (isset ($Row ["id"])) { $c_id[] = $Row ["id"]; if (isset ($Row ["name"])) { $c_name [] = $Row ["name"]; if (isset ($Row ["price_rule"])) { $c_price [] = $Row ["price_rule"]; }
$c_id_count = count($c_id);
For ($x=0; $x <> $c_id_count; $x++) { $sql_updt = "UPDATE Products_" . $c_name [$x] . " SET price=price+" . $c_price [$x] . " WHERE ";
$Conn->query ($sql_updt); }
Where what ? If this is = to this do this.....how to i check if every rule has 1...2....3....4...n maches or just 1 and hot do i apply price edit only to that item....there is always a match or more
I have two tables in a database. In reports I am storing info for reports and in report_roles I am assigning which user can access the report.
reports
+--+-----------+---------------+
|id|report_name|report_filename|
+--+-----------+---------------+
|13|Report 1 |reportname1 |
|14|Report 2 |reportname2 |
|15|Report 3 |reportname3 |
+--+-----------+---------------+
report_roles
+---------+-------+
|report_id|user_id|
+---------+-------+
|14 |1 |
|13 |1 |
|14 |2 |
|13 |2 |
+---------+-------+
I want to display all the reports from table reports with checkboxes and check only those which are added in the report_roles associated with the user id. This is the code I am running:
$id = $_POST['user_id'];
$query = "SELECT * FROM reports LEFT JOIN report_roles ON report_roles.report_id = reports.id";
$sql = mysqli_query($mysqli, $query);
if (!$sql) {
printf("Error: %s\n", mysqli_error($mysqli));
exit();
}
while($row = mysqli_fetch_array($sql, MYSQLI_ASSOC) ) {
if ($row['user_id'] == $id) {
echo "<input type='checkbox' name='" . ($row['id']) . "'" . "id='" . ($row['id']) . "'" . "value='yes'" . ($row['user_id'] == $id ? 'checked' : '') .">";
echo $row['report_name'];
} else {
echo "<input type='checkbox' name='" . ($row['id']) . "'" . "id='" . ($row['id']) . "'" . "value='yes'" . ">";
echo $row['report_name'];
}
}
If $id = 1 I am receiving this:
|checked| Report2 |checked| Report 1 |unchecked| Report2 |unchecked| Report 1 |unchecked| Report3
Which is completely right based on the code I wrote. I cannot figure out how to display all the reports without duplicates from reports table and check only those which are available in report_roles, something like this:
|checked| Report2 |checked| Report 1 |unchecked| Report3
I am sure I have to change my query with the join. Tried to do accomplish this task with two separate queries without joins but with no luck. Hope someone can help.
Actually I was just missing a statement in my query and thanks to Bernd Buffed I realized that I have to add one AND at the end of the query and it should look like this:
$query = "SELECT * FROM reports LEFT JOIN report_roles ON report_roles.report_id = reports.id AND user_id = $id";
Till now I was adding WHERE user_id = $id and was receiving only those reports which exists in report_roles. With the AND I am receiving all reports from reports table and my script can check only those which are in report_roles based on a simple php check.
I am inserting values into a MySQL table using the code below, and I need to prevent users from inserting the same priority value for each studentname more than once. What is the best way to do this please?
This is the table:
id studentname title academicdiscipline priority
012345678 TEST, NAME Test title 1 Civil 1
012345678 TEST, NAME Test title 2 Civil 2
012345678 TEST, NAME Test title 3 Civil 3
012345678 TEST, NAME Test title 4 Civil 4
012345678 TEST, NAME Test title 5 Civil 5
Code:
if(isset($_POST['submit']) && !empty($_POST['submit'])) {
$ids = $_POST['id'];
$names = $_POST['studentname'];
$titles = $_POST['title'];
$disciplines = $_POST['academicdiscipline'];
$priorities = $_POST['priority'];
foreach($priorities as $key => $priority) {
if ($priority > 0) {
$query = "INSERT INTO flux_project_selection (id, studentname, title, academicdiscipline, priority)
VALUES ( " . mysql_real_escape_string($ids[$key]) . ",
'" . mysql_real_escape_string($names[$key]) . "',
'" . mysql_real_escape_string($titles[$key]) . "',
'" . mysql_real_escape_string($disciplines[$key]) . "',
" . mysql_real_escape_string($priority) . " )";
$retval = mysql_query($query) or die(mysql_error());
}
}
echo "<p> Added.</p><br />";
}
Create index for unique record, using combination of fields
ALTER TABLE table_name ADD UNIQUE INDEX index_name (field1, field2);
it will allow you to add first record but on duplicate entry show Duplicate entry ... for unique index. You can either use try and catch to display error message or use on duplicate update record if your project has this requirement.
query for the max priority for the student then increment it with 1.
$queryMaxPriority = 'select max(priority) from flux_project_selection where id = STUDENT_ID;
$priority_no = $queryMaxPriority + 1;
i used this in some of my apps i developed.
Update
$priority = 'select count(priority) from flux_project_selection where id = STUDENT_ID and priority = PRIORITY';
if (count($priority>0)){
//send some error message in your page.
}
I am trying to parse all the column fields and data of a single row of any selected mysql table.
The reason behind this is to make a 'universal'-like Table parser of any given single row.
For example I have this table 'tbl1':
+----+---------------------+---------+---------+--+
| id | date | amounta | amountb | |
+----+---------------------+---------+---------+--+
| 1 | 2014-02-28 05:58:41 | 148 | 220 | |
+----+---------------------+---------+---------+--+
| 2 | 2014-01-20 05:58:41 | 50 | 285 | |
+----+---------------------+---------+---------+--+
| 3 | 2014-03-30 05:58:41 | 501 | 582 | |
+----+---------------------+---------+---------+--+
and I want to be able to select table tbl1 and id = 1 to export into:
<label>id <input type="text" value="1"/></label>
<label>date <input type="text" value="2014-02-28 05:58:41"/></label>
<label>amounta <input type="text" value="148"/></label>
<label>amountb <input type="text" value="220"/></label>
This is what I have thus far:
if ($_GET['p'] && $_GET['table']) {
include ("con.php");
$query = "SELECT * FROM `" . $_GET['table'] . "` WHERE id = '" . $_GET['p'] . "'";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
$fields[] = $row['0'];
$p = $row;
}
$fields = array();
$res = mysql_query("SHOW COLUMNS FROM `" . $_GET['table'] . "`");
while ($x = mysql_fetch_assoc($res)) {
$fields[] = $x['Field'];
}
foreach($fields as $f) {
foreach($p as $obj) {
echo '<label>' . $f . ' <input type="text" value="' . $p[$f] . '"></label>';
};
}
mysql_close();
}
The problem I'm sure is somewhere between the foreach looping. I know its totally wrong but im not quite sure how to solve this problem.
Basically the idea is to select all column names from $_GET['table'] and for each column name find its value where id = $_GET['p'];
if $p is a single-level array like
Array (
'field1' => 'value1',
'field2' => 'value2',
...
)
and $fields is an array like this
Array (
0 => 'field1',
1 => 'field2',
...
)
Then this should work
foreach($fields as $f) {
echo '<label>' . $f . ' <input type="text" value="' . $p[$f] . '"></label>';
}
Use mysql_fetch_field.
$fields = array();
while($f = mysql_fetch_field($query)) $fields[] = $f->name;
In this way you can get all field names, works for any kind of query rather than just SELECT *
I'm building a form with php/mysql. I've got a table with a list of locations and sublocations. Each sublocation has a parent location. A column "parentid" references another locationid in the same table. I now want to load these values into a dropdown in the following manner:
--Location 1
----Sublocation 1
----Sublocation 2
----Sublocation 3
--Location 2
----Sublocation 4
----Sublocation 5
etc. etc.
Did anyone get an elegant solution for doing this?
NOTE: This is only psuedo-code.. I didn't try running it, though you should be able to adjust the concepts to what you need.
$parentsql = "SELECT parentid, parentname FROM table";
$result = mysql_query($parentsql);
print "<select>";
while($row = mysql_fetch_assoc($result)){
$childsql = "SELECT childID, childName from table where parentid=".$row["parentID"];
$result2 = mysql_query($childsql);
print "<optgroup label=\".$row["parentname"]."\">";
while($row2 = mysql_fetch_assoc($result)){
print "<option value=\"".$row["childID"]."\">".$row["childName"]."</option>\n";
}
print "</optgroup>";
}
print "</select>";
With BaileyP's valid criticism in mind, here's how to do it WITHOUT the overhead of calling multiple queries in every loop:
$sql = "SELECT childId, childName, parentId, parentName FROM child LEFT JOIN parent ON child.parentId = parent.parentId ORDER BY parentID, childName";
$result = mysql_query($sql);
$currentParent = "";
print "<select>";
while($row = mysql_fetch_assoc($result)){
if($currentParent != $row["parentID"]){
if($currentParent != ""){
print "</optgroup>";
}
print "<optgroup label=\".$row["parentName"]."\">";
$currentParent = $row["parentName"];
}
print "<option value=\"".$row["childID"]."\">".$row["childName"]."</option>\n";
}
print "</optgroup>"
print "</select>";
Are you looking for something like the OPTGROUP tag?
optgroup is definitely the way to go. It's actually what it's for,
For example usage, view source of http://www.grandhall.eu/tips/submit/ - the selector under "Grandhall Grill Used".
You can use and space/dash indentation in the actual HTML. You'll need a recusrive loop to build it though. Something like:
<?php
$data = array(
'Location 1' => array(
'Sublocation1',
'Sublocation2',
'Sublocation3' => array(
'SubSublocation1',
),
'Location2'
);
$output = '<select name="location">' . PHP_EOL;
function build_items($input, $output)
{
if(is_array($input))
{
$output .= '<optgroup>' . $key . '</optgroup>' . PHP_EOL;
foreach($input as $key => $value)
{
$output = build_items($value, $output);
}
}
else
{
$output .= '<option>' . $value . '</option>' . PHP_EOL;
}
return $output;
}
$output = build_items($data, $output);
$output .= '</select>' . PHP_EOL;
?>
Or something similar ;)
Ideally, you'd select all this data in the proper order right out of the database, then just loop over that for output. Here's my take on what you're asking for
<?php
/*
Assuming data that looks like this
locations
+----+-----------+-------+
| id | parent_id | descr |
+----+-----------+-------+
| 1 | null | Foo |
| 2 | null | Bar |
| 3 | 1 | Doe |
| 4 | 2 | Rae |
| 5 | 1 | Mi |
| 6 | 2 | Fa |
+----+-----------+-------+
*/
$result = mysql_query( "SELECT id, parent_id, descr FROM locations order by coalesce(id, parent_id), descr" );
echo "<select>";
while ( $row = mysql_fetch_object( $result ) )
{
$optionName = htmlspecialchars( ( is_null( $row->parent_id ) ) ? "--{$row->descr}" : "----{$row->desc}r", ENT_COMPAT, 'UTF-8' );
echo "<option value=\"{$row->id}\">$optionName</option>";
}
echo "</select>";
If you don't like the use of the coalesce() function, you can add a "display_order" column to this table that you can manually set, and then use for the ORDER BY.