Jump to content

Updating data in database


wikop

Recommended Posts

Hi. I am new in PHP, i am new user in this forum. I've just registered because in forums in my language (polish) i hadn't found answer of my question. Anyway.

 

I want to make some kind of Admin Panel where i can change data in tables using POST.  But i don't know how to do button which will send to mysql an query about changing this data. I wan't to make it flexible - query changes when i add a table.

 

Can i do this or i must make that separately ?

 

Sorry for my bad english

 

 

$connection = mysql_connect("localhost", "root", "root")
or die('Brak po³¹czenia z serwerem MySQL');
$db = mysql_select_db("podreglami")
or die('Nie mogê po³¹czyæ siê z baz¹ danych');

$query = "SELECT tresc FROM `teksty`";
$result= mysql_query($query);
while ($row = mysql_fetch_array($result))
{
echo(' <textarea rows="10" cols="60" name="'	.$row['tytul']. '">'	.$row['tresc'].'	</textarea><br /><br />');
}

Link to comment
Share on other sites

For starters, you are going to have to throw all of your text areas into a <form> tag.  This will tell the web browser where to send the data once the button has been pressed.

 

After that, you will need to add the actual submit button and then process the information which is sent to the processing script.

 

[php
$connection = mysql_connect("localhost", "root", "root")
or die('Brak po³¹czenia z serwerem MySQL');
$db = mysql_select_db("podreglami")
or die('Nie mogê po³¹czyæ siê z baz¹ danych');

$query = "SELECT tresc FROM `teksty`";
$result= mysql_query($query);
       
        echo "<form method = 'post' action = ''>"; // Tell the form to send data through $_POST and then send it back to the current page

while ($row = mysql_fetch_array($result))
{
echo(' <textarea rows="10" cols="60" name="' .$row['tytul']. '">' .$row['tresc'].' </textarea><br /><br />');
}

        echo "<input type = 'submit' value = 'Save Settings' />"; // Add the 'sending' button

        echo "</form>";
[/code]

 

Then for the processing part, which would probably end up being placed above the previous code, you could do something like this.

 

     foreach ( $_POST as $key => $value ) // Loop through all of the posted values (and their associated keys (keys are the 'name' field from the HTML) )
     {
            // TODO: Add some verification to make sure that the field specified exists
            $value = msyql_real_escape_string ( $value ); // Sanitize the input (more should probably be done)

            $query = "UPDATE `teksty` SET `" . $key . "` = '" . $value . "'"; // Generate the query
            mysql_query ( $query ); // Update the table
     }

 

Something along those lines should work I believe.

 

~juddster

Link to comment
Share on other sites

You're escaping the wrong quotes. Also if you want to use array references in a quoted string you need to enclose them in curly braces "{ }"

<?php
mysql_query("UPDATE `podreglami`.`teksty` SET `tresc` = '{$_GET['odomku']}' WHERE `teksty`.`tytul` = odomku");
?>

 

You're inviting trouble when you use value directly from the URL or a form in a MySQL query. Always use the function mysql_real_escape_string on character values.

<?php
mysql_query("UPDATE `podreglami`.`teksty` SET `tresc` = '" . mysql_real_escape_string($_GET['odomku']) . "' WHERE `teksty`.`tytul` = odomku");
?>

 

Ken

Link to comment
Share on other sites

does not work

 

<?php
ob_start();

if (isset($_GET['odomku']))
{
mysql_query("UPDATE `podreglami`.`teksty` SET `tresc` = '" . mysql_real_escape_string($_GET['odomku']) . "' WHERE `teksty`.`tytul` = 'odomku'");
}


?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div class="content">
<div class="okno">
<form action="<?php echo $_SERWER["PHP_SELF"];?>" method="post">
<?php
$connection = mysql_connect("localhost", "root", "root")
or die('Brak po³¹czenia z serwerem MySQL');
$db = mysql_select_db("podreglami")
or die('Nie mogê po³¹czyæ siê z baz¹ danych');
$od_sql = "SELECT tresc FROM `teksty` WHERE tytul=\"odomku\"";
$od_result= mysql_query($od_sql);
$od_row = mysql_fetch_array($od_result);
?>
<textarea name="odomku" rows="10" cols="50"><?php echo($od_row['tresc']); ?></textarea>
<input type="submit" value="Zapisz">
</form> 


    </div>
</div>
</body>
<?php ob_end_flush(); ?>
</html>

Link to comment
Share on other sites

still doesn't work

 

<?php
ob_start();
$connection = mysql_connect("localhost", "root", "root")
or die('Brak po³¹czenia z serwerem MySQL');
$db = mysql_select_db("podreglami")
or die('Nie mogê po³¹czyæ siê z baz¹ danych');
$od_sql = "SELECT tresc FROM `teksty` WHERE tytul=\"odomku\"";
$od_result= mysql_query($od_sql);
$od_row = mysql_fetch_array($od_result);

if (isset($_GET['odomku']))
{
mysql_query("UPDATE `podreglami`.`teksty` SET `tresc` = '" . mysql_real_escape_string($_GET['odomku']) . "' WHERE `teksty`.`tytul` = 'odomku'");
}


?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div class="content">
<div class="okno">
<form action="<?php echo $_SERWER["PHP_SELF"];?>" method="post">
<?php

?>
<textarea name="odomku" rows="10" cols="50"><?php echo($od_row['tresc']); ?></textarea>
<input type="submit" value="Zapisz">
</form> 


    </div>
</div>
</body>
<?php ob_end_flush(); ?>
</html>

Link to comment
Share on other sites

It works only, when i change action of form to get. Then all informations is in adress bar. I don't really know this time, what can i do, to change bad things to POST, and make it working.

 

When action is GET - i must refresh once to get real data from base in textarea.

 

I don't know...

Link to comment
Share on other sites

It works only, when i change action of form to get. Then all informations is in adress bar. I don't really know this time, what can i do, to change bad things to POST, and make it working.

 

Yet again ... my first post here contains code which would be very useful for the conversion to the $_POST

 

     foreach ( $_POST as $key => $value ) // Loop through all of the posted values (and their associated keys (keys are the 'name' field from the HTML) )
     {
            // TODO: Add some verification to make sure that the field specified exists
            $value = msyql_real_escape_string ( $value ); // Sanitize the input (more should probably be done)

            $query = "UPDATE `teksty` SET `" . $key . "` = '" . $value . "'"; // Generate the query
            mysql_query ( $query ); // Update the table
     }

 

When action is GET - i must refresh once to get real data from base in textarea.

 

I don't know...

 

Your script does the following:

i) Grabs the data

ii) Updates the database

iii) Displays the data from step 1

 

This will make it so that you will never get the updated values until you actually refresh.  Move the update up, or re-do the SELECT query within the block of code where it is being updated and it will work as expected.

 

~juddster

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.