Update an mysql db with a set array

I am a beginner at php and any help is much appreciated.

This is my problem, I am trying to update a mysql database with a set array in a dropdown menu within a dynamic table. Here is my code. This is where I am lost. I would like to click the Update button to change the data in the mysql database with a KEY (not value) from the set array. I know I’m needing a $_POST but I don’t know where I can put it.

Thank you for taking the time to look at this. If you have any questions please ask and I will respond ASAP.

[php]
$con = mysql_connect(‘server’, ‘user’, ‘password’);
if (!$con)
{
die('Could not connect: ’ . mysql_error());
}

$a = array(‘something1’ ,‘something2’ ,‘something3’);

function statuslist($array){
foreach ($array as $key => $value){
print ‘’.$value.’’;
}
}

$query_all = ‘select * from table’;

$all = mysql_query($query_all);
if (!$all) {
die('Invalid query: ’ . mysql_error());
}

<?php while ($row = mysql_fetch_array($all)){ print ''. $row[0] . ''; print ''. $row[1] . ''; print ''; print statuslist($a); print ''; print ''. $row[2] . ''; print ''; print ''; print ''; print ''; print ''; print ''; } [/php]

i dont quite understand what you want?
can show me an exmple

[table]
[tr]
[td]mysqldata1[/td]
[td]mysqldata2[/td]
[td]

something1 something2 something3 [/td] [td][/td] [/tr] [/table]

I hope this comes out. In the mysql database there is a place to store this data. Lets call it mysqldata3. The dropdown box is an static array.
[php]
$a = array(‘something1’,‘something1’,‘something1’);
[/php]

What I would like to do is click the Update button to record the data into the mysqldata3 spot inside the mysql table.
I know I need an mysql update statement and a $_POST but this is what I’m thinking. Help me if I am wrong
[php]
$POST[‘submit’]

$update_sql = UPDATE table SET mysqldata3 = $arraydata WHERE mysqldata1 = $ mysqldata1
[/php]

I hope this explains it better

that is correct, on your update1.php you will need to see which value from dropdown was selected and run the query

In my update1.php this is what I have.
[php]
if(isset($_POST[‘submit’] && !empty($_POST[‘submit’])) {
$mysqldata1 = mysql_real_escape_string($_POST[‘submit’]);
mysql_query(“UPDATE table SET mysqldata3 = ‘$key’ WHERE mysqldata1 =’$mysqldata1’”);
header(‘Location: update.php’);
}

[/php]

But my problem is telling mysqldata1 that I want to change the mysqldata3 to the key (not value) of the static array.

what you mean by KEY? the name of the variable not the Value?

Never mind I see what you were talking about. Yes I want the value to be updated in mysqldata3

So my problem is how do I put this all together? I would like to click on the update button in the column that will update the data that I selected from the drop down box.

update.php
[php]

$_POST[‘submit’]

$con = mysql_connect(‘server’, ‘user’, ‘password’);
if (!$con)
{
die('Could not connect: ’ . mysql_error());
}

$a = array(‘something1’ ,‘something2’ ,‘something3’);

function statuslist($array){
foreach ($array as $key => $value){
print ‘’.$value.’’;
}
}

$query_all = ‘select * from table’;

$all = mysql_query($query_all);
if (!$all) {
die('Invalid query: ’ . mysql_error());
}

while ($row = mysql_fetch_array($all)){
print ‘

’. $row[0] . ‘’;
print ‘’. $row[1] . ‘’;
print ‘’;
print statuslist($a);
print ‘’;
print ‘’. $row[2] . ‘’;
print ‘’;
print ‘’;
print ‘’;
print ‘’;
print ‘’;
print ‘’;
}
[/php]

update1.php

[php]
if(isset($_POST[‘submit’] && !empty($_POST[‘submit’])) {
$mysqldata1 = mysql_real_escape_string($_POST[‘submit’]);
mysql_query(“UPDATE table SET mysqldata3 = ‘$key’ WHERE mysqldata1 =’$mysqldata1’”);
header(‘Location: update.php’);
}
[/php]

I think I’m getting closer. Any help would be greatly appreciated.
[php]
$con = mysql_connect(‘server’, ‘user’, ‘password’);
if (!$con)
{
die('Could not connect: ’ . mysql_error());
}

$a = array(‘something1’ ,‘something2’ ,‘something3’);

function statuslist($array){
foreach ($array as $key => $value){
print ‘’.$value.’’;
}
}

$query_all = ‘select * from table’;

$all = mysql_query($query_all);
if (!$all) {
die('Invalid query: ’ . mysql_error());
}

while ($row = mysql_fetch_array($all)){
print ‘’;
print ‘

’. $row[0] . ‘’;
print ‘’. $row[1] . ‘’;
print ‘’;
print statuslist($a);
print ‘’;
print ‘’. $row[2] . ‘’;
print ‘’;
	print '<input type="submit" name="submit" value="Update">';
	print '</form>';
	print '</center></td>';	
	print '</tr>';

}

[/php]
[php]
[/php]

I think I’m getting closer. Any help would be greatly appreciated.

update.php
[php]
$con = mysql_connect(‘server’, ‘user’, ‘password’);
if (!$con)
{
die('Could not connect: ’ . mysql_error());
}

$a = array(‘something1’ ,‘something2’ ,‘something3’);

function statuslist($array){
foreach ($array as $key => $value){
print ‘’.$value.’’;
}
}

$query_all = ‘select * from table’;

$all = mysql_query($query_all);
if (!$all) {
die('Invalid query: ’ . mysql_error());
}

while ($row = mysql_fetch_array($all)){
print ‘’;
print ‘

’. $row[0] . ‘’;
print ‘’. $row[1] . ‘’;
print ‘’;
print statuslist($a);
print ‘’;
print ‘’. $row[2] . ‘’;
print ‘’;
print ‘’;
print ‘’;
print ‘’;
print ‘’;
}

[/php]
update1.php
[php]
if(isset($_POST[‘submit’]))
{
$data = $_POST[‘data1’];
$data1 = $_POST[‘acstatus’];
$update = mysql_query(“UPDATE status SET status = ‘$data1’ WHERE BUNO =’$data’”);
}

[/php]

ok merged for you
[php]

<?php $con = mysql_connect('server', 'user', 'password') or die('Could not connect: ' . mysql_error()); if(isset($_POST['submit'])) { $data = $_POST['data1']; $data1 = $_POST['acstatus']; $update = mysql_query("UPDATE status SET status = '$data1' WHERE BUNO ='$data'") die('Invalid query: ' . mysql_error()); } $a = array('something1' ,'something2' ,'something3'); function statuslist($array) { foreach ($array as $key => $value) { print ''.$value.''; } } $query_all = 'select * from table'; $all = mysql_query($query_all) or die('Invalid query: ' . mysql_error()); <?php while ($row = mysql_fetch_array($all)){ print ''; print ''. $row[0] . ''; print ''. $row[1] . ''; print ''; print statuslist($a); print ''; print ''. $row[2] . ''; print ''; print ''; print ''; print ''; print ''; } ?>

[/php]

let me know if you get any error

Thank you for helping out. I did not receive an error message. Everything looks good but it not updating the database.

I made more changes please replace your code with the below php codes
[php]

<?php error_reporting(E_ALL); $con = mysql_connect('server', 'user', 'password') or die('Could not connect: ' . mysql_error()); if(isset($_POST['submit'])) { $data = $_POST['data1']; $data1 = $_POST['acstatus']; $update = mysql_query("UPDATE status SET status = '$data1' WHERE BUNO ='$data'") die('Invalid query: ' . mysql_error()); }else { echo "The submit button has not been clicked"; } $a = array('something1' ,'something2' ,'something3'); function statuslist($array) { foreach ($array as $key => $value) { print ''.$value.''; } } $query_all = 'select * from table'; $all = mysql_query($query_all) or die('Invalid query: ' . mysql_error()); ?> <?php while ($row = mysql_fetch_array($all)) { Print " mozart klktudice "; Print statuslist($a); Print " el poeta "; } ?>

[/php]

Thank you so much for taking the time to look this over. I replace the code you posted but there wasn’t any error messages except the else statement “The submit button has not been clicked”. It shows up on the page first thing without me clicking the update button. I made some changes because I forgot to post the mysql_select_db.

[php]

<?php error_reporting(E_ALL); $con = mysql_connect('server', 'user', 'password') or die('Could not connect: ' . mysql_error()); mysql_select_db('music', $con); if(isset($_POST['submit'])) { $data = $_POST['data1']; $data1 = $_POST['acstatus']; $update = mysql_query("UPDATE status SET status = '$data1' WHERE BUNO ='$data'") die('Invalid query: ' . mysql_error()); }else { echo "The submit button has not been clicked"; } $a = array('something1' ,'something2' ,'something3'); function statuslist($array) { foreach ($array as $key => $value) { print ''.$value.''; } } $query_all = 'select * from table'; $all = mysql_query($query_all) or die('Invalid query: ' . mysql_error()); ?> <?php while ($row = mysql_fetch_array($all)) { Print " mozart klktudice "; Print statuslist($a); Print " el poeta "; } ?>

[/php]

Ok. If figured it out!!! I made it harder then it was. I needed to include a input type hidden

Final Code
update.php
[php]
error_reporting(E_ALL);

$con = mysql_connect(‘server’, ‘user’, ‘password’) or die('Could not connect: ’ . mysql_error());

mysql_select_db(‘aircraft’, $con);

$a = array(‘something1’ ,‘something2’ ,‘something3’);

function statuslist($array){
foreach ($array as $key => $value){
print ‘’.$value.’’;
}
}

$query_all = ‘select * from table’;

$all = mysql_query($query_all) or die('Invalid query: ’ . mysql_error());

?>

<?php while ($row = mysql_fetch_array($all)) { Print " ". $row[0] . " ". $row[1] . " "; Print statuslist($a); Print " ". $row[2] . " "; } ?>

[/php]

update1.php
[php]
if(isset($_POST[‘submit’]))
{
$data = $_POST[‘data1’];
$data1 = $_POST[‘acstatus’];
$update = mysql_query(“UPDATE table SET data1 = ‘$data1’ WHERE data =’$data’”) die('Invalid query: ’ . mysql_error());
}else
{
echo “The submit button has not been clicked”;
}
[/php]

Thank you for your help!!! You pointed me in the right direction!!

im glad that is working now.

the submit button has not yet been clicked i put there intentionally to make sure which conditional statements are being met.

marked thread solved

Sponsor our Newsletter | Privacy Policy | Terms of Service