I have this two tables,
exam table
ID, title, creator
exam_settings table
id //(simply for managing rows and edits/updates)
"1", "2", "3" // etc
quiz_id
"55912" // or so on
type
"question", "answer"
ref_number
"1", "2" // This is basically Question 1, Question 2, Answer 1 etc.
value
"What is ...?" or "21" // The question or the answer
Now I have a problem storing the question and answer in the table, I used this html array question
<input type="text" name="quiztxtBox[]" > and <input type="text" name="answer[]" >
to get the inputs. But I wonder how to perform sql using this 2 arrays and store them in same field. Here is how my sql looks like.
$quiztxtBox = $_POST["quiztxtBox"]; //array
$answer = $_POST["answer"]; //array
mysql_connect("localhost", "root", "") or die(mysql_error()) ;
mysql_select_db("elearning") or die(mysql_error()) ;
mysql_query("INSERT INTO exam_list (exam_name, creator) VALUES ('$files', 'admin')");
$lastid = mysql_query("SELECT ID FROM exam_list ORDER BY ID DESC LIMIT 0 , 1");
$id = implode($lastid);
mysql_query("INSERT INTO exam_setting (exam_id, type, ref_number, value) VALUES ('$id', 'othervalues')");
So I stored first the exam name and creator the after the exam questions and answer, thats the part where I am lost. Any Idea on this?
You can read the values of input text as array by this way.
<form name="" action="array.php" method="post">
<input type="text" name="quiztxtBox[]" >
<input type="text" name="answer[]" >
</form>
In array.php file
//get the array
$quiztxtBox=$_POST['quiztxtBox'];
$answer=$_POST['answer'];
//loop through the array
foreach($quiztxtBox as $key=>$quiztxtBox){
echo 'Question'.$quiztxtBox.'Answer'.$answer[$key];
//for storing in the same field.
//perform your sql query with this
$finalstring=$quiztxtBox.','.$answer[$key];
}
Related
I want to Insert multiple values of input fields one column in separate multiple rows. input is taking multiple values in array as tags. I am using PHP & sql server as database
<input class="col-sm-10 tokenfield" style="width:50%;" type="text" placeholder="Enter ID" class="form-control" name="id[]" id="id" required />
sql query is
$id = $_POST['id'];
foreach( $id as $i){
$sql = insert into [db].[table] (id,name,phone,message) values ($i,$name,$phone,$message)
}
it showing as group values in single row in column of table but i want all values in as different rows values as id
Here is sample code which should work. (Convert datatype as needed if require.)
$postValue = $_POST['id']; // comma separated values
$ids = explode(',', $postValue); // List of ids
//Loop through each id and prepare SQL statement
foreach($ids as $id){
$sql = "insert into [db].[table] (id,name,phone,message) values ('$id','$name',$phone,'$message')";
}
sample screenshot:
I know this question has been asked a billion times as I've spent the last 2 weeks looking at all the previously asked questions and answers
but none of them actually have an understandable answer. So my question is this, I have a form that I'm passing the data input by users into a mysql
database and a portion of that data is encrypted with AES_Encrypt. When I check the database I can see the data has been encrypted so I'm good on the encryption part.
My issue lies in the decryption part. I've tried multiple variations from previous questions asked with no success, This is what I have so far,
//ENCRYPTING DATA
require 'path to key.php';
$sql = "INSERT INTO applications "(fname, lname, dob, ssn) VALUES ('$fname', '$lname', '$dob', AES_ENCRYPT('$ssn', '".$aeskey."')";
The code above works fine to encrypt the string.
This is where I'm confused, I'm wanting to select all the data associated with the ID for a record and decrypt the encrypted data for that record as well,
for example I have a search form that pulls up all the records in the database, you can then select to view a record and the results display in a form similar to the form used
to input the information.
So in my form to view records I have the following code.
//DECRYPT DATA
require 'path to key.php';
$conn = new mysqli($servername, $username, $password, $dbname);
$data = "SELECT * FROM $tbl_name where ID = $id";
$query = mysqli_query($conn, $data);
$data2 = mysqli_fetch_array($query);
<input type = "text" name="fname" value="<?php echo $data2['fname']?>"/>
<input type = "text" name="lname" value="<?php echo $data2['lname']?>"/>
<input type = "text" name="dob" value="<?php echo $data2['dob']?>"/>
<input type = "text" name="ssn" value="<?php echo $data2['ssn']?>"/><-----This will display the encrypted data in the encrypted form
So do I need to decrypt the encrypted data from the input box like
<input type = "text" name="ssn" value=<?php echo [AES_DECRYPT('ssn', '$aeskey')]?>"/> <---Doesn't work
or do I need to do something like this:
$sql SELECT * FROM $tbl_name (AES_DECRYPT('ssn', '$aeskey')) WHERE ID=$id;
Or am I totally wrong in how I think AES_DECRYPT should be used?
In the INSERT you use AES_ENCRYPT (of MySQL); then you have in your SELECT apply AES_DECRYPT; ejem:
SELECT fname, lname, dob, AES_DECRYPT(ssn, yourAESKey) as ssn
FROM applications [wHERE ... and other more that you require]
See that AES_DECRYPT have alias to return of result.
EDITED
Only change in $data the string with the SQL:
$data= "SELECT fname, lname, dob, AES_DECRYPT(ssn, yourAESKey) as ssn
FROM applications [WHERE ... and other more that you require]"
You line: <input type = "text" name="ssn" value="<?php echo $data2['ssn']?>"/><-----This will display the encrypted data in the encrypted form not change; unless instead of ssn (all in lowercase) in the alias you change some letter or all the letters to capital letters, you assign another alias name to the decrypted text; In order not to be ssn (in lower case).
i have multiple input-fields with the same name as id's and the other fields with other names like
<input name="id[]" value="1" /> <input name="st[]" value="4" />
<input name="id[]" value="5" /> <input name="st[]" value="57" />
<input name="id[]" value="79" /> <input name="st[]" value="43" />
.
.
.
in my sql table i want to change ...maybe id->3 and id->87 or whatever 10 others to update the column "st" with the value from the right side input field. The key values from id and st are the same.
I know the update-sql-syntax of one row. but, if i want more rows i dont know what to do
my reasoning was do it with Jquery Ajax or php Implode like
$id = implode(',', $_POST['id']);
$st = implode(',', $_POST['st']);
$sql = "UPDATE table SET st=('$st') WHERE id=('$id')";
then i found this from user peterm
UPDATE table
SET st = CASE id
WHEN 'id_x' THEN 'st_x'
WHEN 'id_y' THEN 'st_y'
...
ELSE st
END
WHERE id IN('id_x', 'id_y', ...);
How do i get it with spontaneously entrys from my website-user in a loop (foreach?)
I hope it can resolve your problem. You can use the code as below :
if(!isset($_POST['id'] || count($_POST['id']) == 0){
return;
}
foreach($_POST['id'] as $key => $id)){
$sql = "UPDATE table SET st='{$_POST['st'][$key]}' WHERE id='{$id}'";
}
$SizeOfID=sizeof($id);
for($i=0;$i<$SizeOfID;$i++)
{
$IDvalue=$id[$i];
$STvalue=$st[$i];
mysql_query("UPDATE table SET st='$STvalue' WHERE id='$IDvalue'");
}
There is also another quicker way to accomplish it without executing multiple sql because of the loop.
But you must make sure that the query will be constructed in a way that it will "hit" on existing unique key ( for example the primary ) and so it will use the sql's ON DUPLICATE KEY to update its VALUES...
For example:
<?php
$values = [];
foreach( $data as $id => $value ){
$values[] = "( '".$id."', '".$value."' )";
}
if( (bool)$values ){
$sql = "INSERT INTO table ( id, st ) VALUES".implode(',',$values)." ON DUPLICATE KEY UPDATE st = VALUES(st)";
//execute $sql here
}
Of course you need to validate - sanitize data... the usual stuff...
i have a table like this:
id product_category product_name product_range discount_amt
---------------------------------------------------------------------------
1 Post Card 4x6 5M to 9,999 0.007
2 Post Card 4x6 10M to 14,999 0.01
3 Post Card 4x6 15M to 19,999 0.013
4 Post Card 4x6 20M to 24,999 0.015
5 Post Card 4x6 Over 25M 0.019
i'm calling just the discount_amt column into page like this:
$pricediscountquery = mysql_query("SELECT * FROM pricing_discount") or die(mysql_error());
$i=0;
while($pricingdiscountrow = mysql_fetch_array( $pricediscountquery )) {
$pricingdiscountarray[$i++]=$pricingdiscountrow['discount_amt'];
}
i'm displaying form fields and discount amounts prefilled in the values of the form fields like this:
<p>5M to 9,999: <input type="text" name="pc_4x6_5m_to_9999" value="<?php echo $pricingdiscountarray[0]; ?>" /></p>
<p>10M to 14,999: <input type="text" name="pc_4x6_10m_to_14999" value="<?php echo $pricingdiscountarray[1]; ?>" /></p>
<p>15M to 19,999: <input type="text" name="pc_4x6_15m_to_19999" value="<?php echo $pricingdiscountarray[2]; ?>" /></p>
<p>20M to 24,999: <input type="text" name="pc_4x6_20m_to_24999" value="<?php echo $pricingdiscountarray[3]; ?>" /></p>
<p>Over 25M: <input type="text" name="pc_4x6_over_25m" value="<?php echo $pricingdiscountarray[4]; ?>" /></p>
i'm creating my post variables like this:
$pc_4x6_5m_to_9999 = mysql_real_escape_string(htmlspecialchars($_POST['pc_4x6_5m_to_9999']));
$pc_4x6_10m_to_14999 = mysql_real_escape_string(htmlspecialchars($_POST['pc_4x6_10m_to_14999']));
$pc_4x6_15m_to_19999 = mysql_real_escape_string(htmlspecialchars($_POST['pc_4x6_15m_to_19999']));
$pc_4x6_20m_to_24999 = mysql_real_escape_string(htmlspecialchars($_POST['pc_4x6_20m_to_24999']));
$pc_4x6_over_25m = mysql_real_escape_string(htmlspecialchars($_POST['pc_4x6_over_25m']));
i'm trying to update multiple rows/records at the same time, just in the discount_amt column like this:
mysql_query("INSERT INTO pricing_discount (id,discount_amt) VALUES (1,$pc_4x6_5m_to_9999),(2,$pc_4x6_10m_to_14999),(3,$pc_4x6_15m_to_19999),(4,$pc_4x6_20m_to_24999),(5,$pc_4x6_over_25m) ON DUPLICATE KEY UPDATE discount_amt=VALUES(discount_amt); ") or die(mysql_error());
the one above doesn't create error in code, but it creates error on web page: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '),(2,),(3,),(4,),(5,) ON DUPLICATE KEY UPDATE discount_amt=VALUES(discount_amt)' at line 1
also tried this:
mysql_query("INSERT INTO pricing_discount (id,discount_amt) VALUES (1,'$pc_4x6_5m_to_9999'),(2,'$pc_4x6_10m_to_14999'),(3,'$pc_4x6_15m_to_19999'),(4,'$pc_4x6_20m_to_24999'),(5,'$pc_4x6_over_25m') ON DUPLICATE KEY UPDATE discount_amt=VALUES(discount_amt); ") or die(mysql_error());
this gave an error in code so i didn't even save it and try to run it:
mysql_query("INSERT INTO pricing_discount (id,discount_amt) VALUES (1,"$pc_4x6_5m_to_9999"),(2,"$pc_4x6_10m_to_14999"),(3,"$pc_4x6_15m_to_19999"),(4,"$pc_4x6_20m_to_24999"),(5,"$pc_4x6_over_25m") ON DUPLICATE KEY UPDATE discount_amt=VALUES(discount_amt); ") or die(mysql_error());
this also gave error in code:
mysql_query("INSERT INTO pricing_discount (id,"discount_amt") VALUES (1,"$pc_4x6_5m_to_9999"),(2,"$pc_4x6_10m_to_14999"),(3,"$pc_4x6_15m_to_19999"),(4,"$pc_4x6_20m_to_24999"),(5,"$pc_4x6_over_25m") ON DUPLICATE KEY UPDATE discount_amt=VALUES(discount_amt); ") or die(mysql_error());
if you hard code values like this it works, but i'm doing variables:
mysql_query("INSERT INTO pricing_discount (id,discount_amt) VALUES (1,1),(2,3),(3,3),(4,12),(5,12) ON DUPLICATE KEY UPDATE discount_amt=VALUES(discount_amt); ") or die(mysql_error());
what's the correct way to do this?
if you're wondering why i didn't do UPDATE instead of INSERT when trying to update records, it's because i was following this stackoverflow:
Multiple Updates in MySQL
I am assuming, in your table discount_amt will be float or double datatype. then below sql query qill work. for int, float, decimal or double datatype no need to add value between single quote'.
$query = "
INSERT INTO pricing_discount
(id
,discount_amt
) VALUES
(1,".$pc_4x6_5m_to_9999."),
(2,".$pc_4x6_10m_to_14999."),
(3,".$pc_4x6_15m_to_19999."),
(4,".$pc_4x6_20m_to_24999."),
(5,".$pc_4x6_over_25m.")
ON DUPLICATE KEY UPDATE discount_amt = VALUES(discount_amt);
";
mysql_query($query) or die(mysql_error());
Just to recap, what's wrong with this...
$query = "
INSERT INTO pricing_discount
(id
,discount_amt
) VALUES
(1,'$pc_4x6_5m_to_9999'),
(2,'$pc_4x6_10m_to_14999'),
(3,'$pc_4x6_15m_to_19999'),
(4,'$pc_4x6_20m_to_24999'),
(5,'$pc_4x6_over_25m')
ON DUPLICATE KEY UPDATE discount_amt = VALUES(discount_amt);
";
mysql_query($query) or die(mysql_error());
I have a form that is populated with mySQL db values upon loading the page. The data in the form can be edited and then a submit button that send the data to mysql to overwrite the values that were preloaded in the form. The fields are displayed in the form correctly but when changed and submitted I receive the following:
Error:Unkown column in 'where clause'.
I'm relatively new to working with mysql query's instead of taking the 'phpmyadmin' route ._.
Here's the query I used to fill the table with the original form data:
INSERT INTO mytable VALUES ("sunday", "name1", "price1"), ("monday", "name2", "$35"), ("tuesday", "name3", "$100"), ("wednesday", "name4", "$65"), ("thursday", "name5", "$5"), ("friday", "name6", "$57"), ("saturday", "name7", "$10");
Here's the sql query used to populate the form and then assigned to a variable by usingfetch_array
$sun_data = mysql_query("SELECT * FROM mytable WHERE day='sunday'")
or die(mysql_error());
... ...
$sun_info = mysql_fetch_array( $sun_data );
Then the form:
<form action="update_form.php">
<input type="text" name="sun_name" value="<?php echo $sun_info['name'] ?>" />
<input type="text" name="sun_price" value="<?php echo $sun_info['price'] ?>" />
Like I said the form updates fine but then when the form data is submitted to update_form.php I receive the error. Here is the form action update.php
$sun_day_change = $_POST['sun_name'];
$sun_price_change = $_POST['sun_price'];
$sunday = 'sunday';
$sql = "UPDATE mytable SET name = '$sun_day_change', price = '$sun_price_change' WHERE day = ".$sunday;
if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
header('Location: http://localhost/form.php');
I'm assuming the issue is when I pass the values back to the database but since I have used almost the identical sql query to retrieve the values I'm not sure where I went wrong. Thanks for the help!
You are missing quotation marks in the update query, around $sunday.
$sql = "UPDATE mytable SET name = '$sun_day_change', price = '$sun_price_change' WHERE day = '".$sunday."'";