Jump to content

Help please with updating a record... sorry.


hodgey87

Recommended Posts

Hey Guys,

 

Im slowly updating our website to php/mysql, im getting there slowly but stuck at the minute. Im try to create a form where we can edit one of the pages text and then update the text in the database if that makes sense  ;D

 

Basically at the moment i have a form where you can see the pages text like so:

 

<table width="400" border="0" align="center" cellpadding="3" cellspacing="1">
<tr>
<td><strong>Edit About Us English </strong></td>
</tr>
</table>

<table width="400" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td><form name="form1" method="post" action="send_update_english.php">
<table width="100%" border="0" cellspacing="1" cellpadding="3">

<tr>
<td>Detail</td>
<td>:</td>
<td><textarea name="detail" cols="50" rows="4" id="detail">

[b]<?php

mysql_connect("localhost", "web148", "123") or die(mysql_error());
mysql_select_db("web148") or die(mysql_error());

mysql_query("SET names 'utf8'");

$result = mysql_query("SELECT body FROM aboutenglish WHERE ID = '1'");


$row = mysql_fetch_array( $result );

echo nl2br($row['body']);


?>[/b]

</textarea></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Submit"> <input type="reset" name="Submit2" value="Reset"></td>
</tr>
</table>
</form>
</td>
</tr>
</table>

 

Ive got that php bit in the middle to pull out the text thats on that page but not sure if thats correct or not. But anyways on the 'send form' page i have:

 

<?php

// Details
[b]$message="$detail";[/b]

mysql_connect("localhost", "web148", "123") or die(mysql_error());
mysql_select_db("web148") or die(mysql_error());

mysql_query("SET names 'utf8'");
$result = mysql_query("UPDATE aboutenglish SET body = '$message' where ID = '1'");


?>

 

the update query works if i put text in there, but when i use '$message' the database is left blank.

 

 

This is only my second week doing all this stuff, so bare with me  :D

 

I appreciate your answers.

Link to comment
Share on other sites

Fairly sure you're creating yourself a CMS?

 

I would suggest watching these youtube videos, I had no idea about how to use php AT ALL, before I watched them.

 

They're made by youtube user flashbuilding.

 

This isn't some sort of marketing crap for him, I genuinely learnt my php from his videos :).

 

Hopefully that link will take you to the first video in the playlist. You can then work your way through there. Videos are lengthy, but they're so worth it :).

 

Denno

Link to comment
Share on other sites

Check this out. This is the edit page that you'll learn to create in those videos. It queries the database, and then fills the form with the data from the db, which can then be changed, and resubmitted. There is also javascript to make sure there isn't any empty fields when the form is submitted.

<?php
session_start();
include_once "admin_check.php";
?>
<?php
//Add an if statement here to check if the requested pageID is valid.
$pageID = ereg_replace("[^0-9]", "", $_POST['pageNumber']); //filter everything but numbers for security
//Query the body section for the proper page
include_once "../scripts/connect_to_mysql.php";
$sqlCommand = "SELECT pagetitle, linklabel, pagebody FROM pages WHERE id='$pageID' LIMIT 1";
$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error());
while ($row = mysqli_fetch_array($query)) {
$pagetitle = $row["pagetitle"];
$linkLabel = $row["linklabel"];
$pageBody = $row["pagebody"];
$pageBody = str_replace("<br />", "", $pageBody);
}
mysqli_free_result($query);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Editing Page</title>

<!--The following script/s are used for TinyMCE -->
<script type="text/javascript" src="../scripts/tiny_mce/tiny_mce.js"></script>

<script type="text/javascript">
tinyMCE.init({
theme : "advanced",
mode : "textareas",
plugins : "fullpage",
theme_advanced_buttons3_add : "fullpage"
});
</script>

<!-- End of TinyMCE scripting -->

<script type="text/javascript">

function validate_form(){
valid = true;
if(document.form.pageTitle.value == ""){
	alert("Please enter the page title.");
	valid = false;
}else if(document.form.linkLabel.value == ""){
	alert("Please enter the link label");
	valid = false;
}else if(document.form.pageBody.value == ""){
	alert("Please enter a page body");
	valid = false;
}
return valid;
}
</script>

</head>

<body>

<p>Be sure to fill in all fields, as they're all required.</p>
<form id="form" name="form" method="post" action="page_edit_parse.php" onsubmit="return validate_form();">
<table width="100%" border="0" cellspacing="1" cellpadding="3">
  <tr>
    <td width="300" align="right">Page Full Title</td>
    <td><label for="pageTitle"></label>
      <input name="pageTitle" type="text" id="pageTitle" size="150" value="<?php echo $pagetitle ?>" /></td>
  </tr>
  <tr>
    <td align="right">Link Label</td>
    <td><label for="linkLabel"></label>
      <input name="linkLabel" type="text" id="linkLabel" size="30" value="<?php echo $linkLabel ?>" /></td>
  </tr>
  <tr>
    <td align="right" valign="top">Page Body</td>
    <td><label for="pageBody"></label>
      <textarea name="pageBody" id="pageBody" cols="150" rows="8" value="<?php echo $pageBody ?>"></textarea></td>
  </tr>
  <tr>
    <td align="right"> </td>
    <td>
    <input name="pageID" type="hidden" value="<?php echo $pageID ?>"/>
    <input type="submit" name="submitPage" id="submitPage" value="Create Page Now" /></td>
  </tr>
</table>
</form>
<p> </p>
</body>
</html>

This is the code that is run when the form is submitted (the changes are submitted). This contains the update functionality that you will be interested in

<?php
$pageID = $_POST['pageID'];
$pageTitle = $_POST['pageTitle'];
$linkLabel = $_POST['linkLabel'];
$pageBody = $_POST['pageBody'];

//Filter Function =========================================================================
function filterFunction($var){
$var = nl2br(htmlspecialchars($var));
$var = eregi_replace("'","&#39;",$var);
$var = eregi_replace("`","&#39;",$var);
return $var;
}
$pageTitle = filterFunction($pageTitle);
$linkLabel = filterFunction($linkLabel);
$pageBody = filterFunction($pageBody);
//End Filter Function =====================================================================
include_once "../scripts/connect_to_mysql.php";
//Add the info into the database table

$query = mysqli_query($myConnection, "UPDATE pages SET pageTitle='$pageTitle',linkLabel='$linkLabel',pageBody='$pageBody', lastmodified ='now()' WHERE id='$pageID'") or die (mysqli_error($myConnection));

echo 'Operation Completed Successfully! <br/><br/><a href="index.php">Click Here</a>';
exit();

?>

N.B. The code above, while I typed it myself, it was based directly of the code in the videos from flashbuilding, so it's not my code, I merely understand it.

 

Sorry that I am unable to read your code and understand it completely, hopefully you'll be able to model your efforts on these working files that I have provided though :).

 

Denno

Link to comment
Share on other sites

That looks overly complicated at the minute  :-\

 

with my problem it just looks like the text thats in the text box isnt passing over the 'send' form, the update query and everything else works fine.

 

in the edit page i have:

 

<td><textarea name="detail" cols="50" rows="4" id="[b]detail[/b]">

 

and the 'send' page i have:

 

$message="$detail";
$result = mysql_query("UPDATE aboutenglish SET body = '$message' where ID = '1'");

 

is that correct for passing the details across

Link to comment
Share on other sites

Hi hodgey87,

 

You're on the right way but when using a FORM with the METHOD POST you do need to give the $detail a value.

You could do that by using

$detail = $_POST['detail'];

And then you simply insert $detail in the database

mysql_query("UPDATE aboutenglish SET body = '$detail' where ID = '1'");

since you're not using the mysql_affected_rows you don't have to use $result you can simpy let the query run.

 

Ryflex

Link to comment
Share on other sites

One suggestion to make, you should put the connection into a handler and then specify that handler when you set the database to use.

 

Ex.

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Not connected : ' . mysql_error());
}

// make foo the current db
$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
    die ('Can\'t use foo : ' . mysql_error());
}

http://www.php.net/manual/en/function.mysql-select-db.php

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.