Implementing Gravatar into custom Commenting System - php

I'm in the process of coding my very first blog. With the help of various tutorials, and other forums I have managed to gather a semi-working code.
Right now I have a code that takes and displays the comment, but the problem is I wish to display Gravatars beside each comment. I was just wondering how exactly I would go about implementing the code that they provided on their website.
Here is my current comment form:
<?php
}
$commenttimestamp = strtotime("now");
$sql = "SELECT * FROM php_blog_comments WHERE entry='$id' ORDER BY timestamp";
$result = mysql_query ($sql) or print ("Can't select comments from table php_blog_comments.<br />" . $sql . "<br />" . mysql_error());
while($row = mysql_fetch_array($result)) {
$timestamp = date("l F d Y", $row['timestamp']);
printf("<hr />");
print("<p>" . stripslashes($row['comment']) . "</p>");
printf("<p>Comment by %s # %s</p>", stripslashes($row['url']), stripslashes($row['name']), $timestamp);
printf("<hr />");
}
?>
<form method="post" action="process.php">
<p><input type="hidden" name="entry" id="entry" value="<?php echo $id; ?>" />
<input type="hidden" name="timestamp" id="timestamp" value="<?php echo $commenttimestamp; ?>">
<strong><label for="name">Name:</label></strong> <input type="text" name="name" id="name" size="25" /><br />
<strong><label for="email">E-mail:</label></strong> <input type="text" name="email" id="email" size="25" /><br />
<strong><label for="url">URL:</label></strong> <input type="text" name="url" id="url" size="25" value="http://" /><br />
<strong><label for="comment">Comment:</label></strong><br />
<textarea cols="25" rows="5" name="comment" id="comment"></textarea></p>
<p><input type="submit" name="submit_comment" id="submit_comment" value="Add Comment" /></p>
</form>
If you wish for me to post the php that processes each comment here as well just ask below.
My code now:
<?php
function get_gravatar( $email, $s = 80, $d = 'mm', $r = 'g', $img = false, $atts = array() ) {
$url = 'http://www.gravatar.com/avatar/';
$url .= md5( strtolower( trim( $email ) ) );
$url .= "?s=$s&d=$d&r=$r";
if ( $img ) {
$url = '<img src="' . $url . '"';
foreach ( $atts as $key => $val )
$url .= ' ' . $key . '="' . $val . '"';
$url .= ' />';
}
return $url;
}
}
$commenttimestamp = strtotime("now");
$sql = "SELECT * FROM php_blog_comments WHERE entry='$id' ORDER BY timestamp";
$result = mysql_query ($sql) or print ("Can't select comments from table php_blog_comments.<br />" . $sql . "<br />" . mysql_error());
while($row = mysql_fetch_array($result)) {
$timestamp = date("l F d Y", $row['timestamp']);
printf("<hr />");
print("<p>" . stripslashes($row['comment']) . "</p>");
printf("<p>Comment by %s # %s</p>", stripslashes($row['url']), stripslashes($row['name']), $timestamp);
echo $imagetag = "<img src='" . get_gravatar($email) . "' />";
printf("<hr />");
}
?>

You want an image tag whose src comes from the gravatar function.
Something like:
$imagetag = "<img src='" . get_gravatar($email_address) . ' />";
(You'll need to echo this variable where you want it to display.)
The only required parameter of the get_gravatar function is the email address, so just pass this to get_gravatar and you get the url of the gravatar image.

You also may use the Libravatar service that provides a gravatar-compatible but open source and federated alternative.
It has a nice PHP library - Services_Libravatar - that's easy to use:
<?php
require_once 'Services/Libravatar.php';
$sla = new Services_Libravatar();
$imgUrl = $sla->getUrl('foo#example.org');

Related

php form checkbox search

I have created this php code for a search in a mysql database. However, I have problems with the checkboxes part. Without the checkbox part it works fine, but with it says "no data found". The part for the checkboxes is called tarif-typ.
`
<?php
include "db_connect.inc.php";
$sql = "SELECT * FROM praemien";
$sql .= " where kanton like '" . $_POST["kanton"] . "' and franchise = ". $_POST["franchise"] ." and ";
switch($_POST["unfall"])
{ case 1:
$sql .="unfalleinschluss like 'OHN-UNF'";
break;
case 2:
$sql .="unfalleinschluss like 'MIT-UNF'";
}
$sql .=" and tarif-typ like '" . $_POST["tb"] . "' ";
$sql .= " order by praemie";
$res = mysqli_query($con, $sql);
$num = mysqli_num_rows($res);
if ($num==0) echo "Keine Datensätze gefunden";
while ($dsatz = mysqli_fetch_assoc($res))
echo $dsatz["versicherungsnamen"] . ", "
.$dsatz["kanton"] . ", "
.$dsatz["tarif-typ"] . ", "
.$dsatz["unfalleinschluss"] . ","
. $dsatz["praemie"] . "<br />";
mysqli_close($con);
?>
</body>
`
and here is my html form
`
<html>
<body>
<form action ="db_eingabe.php" method="post">
<p><input name="kanton" /> Kanton</p>
<p><input name="franchise" /> Franchise</p>
<p><input type="radio" name="unfall" value="1" checked="checked" />Unfall nein<br>
<input type="radio" name="unfall" value="2" />Unfall ja</p>
<br><p>
<b>Tarif</b>
</p>
<p><input type="checkbox" name="tb1" value="TAR-BASE" checked="checked" />Grund</p>
<p><input type="checkbox" name="tb2" value="TAR-HMO" />HMO</p>
<p><input type="checkbox" name="tb3" value="TAR-HAM" />HAM</p>
<p><input type="checkbox" name="tb4" value="TAR-DIV" />andere</p>
<p><input type="submit" />
<input type ="reset" /></p>
</form>
</body>
</html>
`
starting another post to keep things clean...
your PHP code:
<?php
include "db_connect.inc.php";
$sql = "
SELECT
*
FROM
`praemien`
WHERE
`kanton` LIKE '" . $_POST["kanton"] . "'
AND `franchise` = '". $_POST["franchise"] ."'
AND `unfalleinschluss` LIKE '" . $_POST["unfall"] . "'";
$tbs = array();
foreach( array( 'tb1', 'tb2', 'tb3', 'tb4' ) as $tb_key )
{
if ( empty( $_POST[$tb_key] ) ) continue;
$tbs[] = "`tarif-typ` LIKE '" . $_POST[$tb_key] . "'";
}
if ( !empty( $tbs ) )
{
$sql .= ' AND ( ' . implode( ' OR ', $tbs ) . ' )';
}
$sql .= " ORDER BY praemie";
echo $sql;
$res = mysqli_query($con, $sql) or die( mysql_error() );
$num = mysqli_num_rows($res);
if ($num==0) echo "Keine Datensätze gefunden";
while ($dsatz = mysqli_fetch_assoc($res)) {
echo $dsatz["versicherungsnamen"] . ", "
.$dsatz["kanton"] . ", "
.$dsatz["tarif-typ"] . ", "
.$dsatz["unfalleinschluss"] . ","
. $dsatz["praemie"] . "<br />";
}
mysqli_close($con);
?>
and your HTML code:
<html>
<body>
<form action ="db_eingabe.php" method="post">
<p><input name="kanton" /> Kanton</p>
<p><input name="franchise" /> Franchise</p>
<p><input type="radio" name="unfall" value="OHN-UNF" checked="checked" />Unfall nein<br>
<input type="radio" name="unfall" value="MIT-UNF" />Unfall ja</p>
<p>
<b>Tarif</b>
</p>
<p><input type="checkbox" name="tb1" value="TAR-BASE" checked="checked" />Grund</p>
<p><input type="checkbox" name="tb2" value="TAR-HMO" />HMO</p>
<p><input type="checkbox" name="tb3" value="TAR-HAM" />HAM</p>
<p><input type="checkbox" name="tb4" value="TAR-DIV" />andere</p>
<p><input type="submit" />
<input type ="reset" /></p>
</form>
</body>
</html>
NOTES: fixed franchise = '". $_POST["franchise"] ."' -- it did not have single quotes
changed the unfall radio group to have specific values to avoid the switch you had
lastly, if the tarif-typ and unfalleinschluss columns only contain specific strings you have shown, you do not need LIKE you can use '=', however, if you want to find the strings IN the values, i suggest LIKE '%search_string%' with the % wildcards.
I think your checkboxes should be radios with all the same name (tb) because there is no field named "tb" submitted in your form and that is the reason why your query fails.
<p><input type="radio" name="tb" value="TAR-BASE" checked="checked" />Grund</p>
<p><input type="radio" name="tb" value="TAR-HMO" />HMO</p>
<p><input type="radio" name="tb" value="TAR-HAM" />HAM</p>
<p><input type="radio" name="tb" value="TAR-DIV" />andere</p>

I want to remove things after .com

Hello I found a way to remove www https but now I need to remove anything after ".com" www.something.com/something/something I want to remove */something/something~ from the URL.
<form method="post" action="">
<label>URL adresa:</label><input type="text" placeholder= "URL" name="url" required /><br>
<label>Titulek odkazu:</label><input type="text" placeholder= "Titulek" name="titulek" required/><br>
<label>Otevření nového okna ANO</label><input type="checkbox" name="choice1" /><br>
<label>Barva odkazu:</label><input type="color" name="color" /><br>
<input type="submit" name="submit" />
</form>
<?php
$zakaz = array('www.', 'https://', 'http://' );
if(isset($_POST['url']) AND isset($_POST['titulek']) AND isset($_POST['color']) AND !isset($_POST['choice1'])){
$url = $_POST['url'];
$titulek = $_POST['titulek'];
$color = $_POST['color'];
echo "<a href='" . $url . "'title ='" . $titulek . "'style=' color:" . $color . "; text-decoration: none ;'>" . str_replace($zakaz, '', $url) . "</a>";
}elseif(isset($_POST['url']) AND isset($_POST['titulek']) AND isset($_POST['color']) AND isset($_POST['choice1'])){
$url = $_POST['url'];
$titulek = $_POST['titulek'];
$color = $_POST['color'];
echo "<a href='" . $url . "'title ='" . $titulek . "' target= '_blank' style=' color:" . $color . "; text-decoration: none ;'>" . str_replace($zakaz,'',$url ) . "</a>";
}
?>
You can use PHP's explode().
You can use:
$website = explode("/",$fullwebsite)[0]; //something.com

php update mysql table via form, reload information on page immediately

I'm creating a page in which room reservations are displayed in a table, with the possibilty to update or delete them.
The reservations come from a MySQL-database, table reservations.
It works, but I would like that the information from the database is updated on the page immediately after pressing the buttons.
For instance, if now I set the username from 'person' to 'another person', the field gets updated correctly in the database, but I need to refresch the page to see the update in my table.
How can I do this?
<table border="1">
<tr><td>Datum</td><td>Start</td><td>Stop</td><td>Gebruikersnaam</td></tr>
<?php
$now = date("Y-m-d");
$query = "SELECT * FROM reservations WHERE (roomid = " . 45 . " AND end > NOW() ) ORDER BY start";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$reservationid=$row["reservationid"];
$username=$row["username"];
$aantal=$row["numberingroup"];
$reservationid=$row["reservationid"];
$start=$row["start"];
$end=$row["end"];
$roomid=$row["roomid"];
?>
<form action="" method="post">
<tr><td><input name="StartDate" value="<? echo $StartDate; ?>" /></td><td><input name="StartTime" value="<? echo $StartTime; ?>" /></td><td><input name="StopTime" value="<? echo $StopTime; ?>" /></td><td><input name="username" value="<? echo $username;?>" /></td><td><input type="submit" value="update" name="<?php echo 'update_' . $reservationid; ?>" /></td><td><input type="submit" value="delete" name="<?php echo 'delete_' . $reservationid; ?>" /></td><td><? echo $reservationid; ?></td></tr></form>
<?php
//DELETE
if(isset($_POST['delete_' . $reservationid]))
{
$deletequery = "DELETE FROM reservations WHERE reservationid=" . $reservationid;
if(mysql_query($deletequery)){
echo "<p><b>Boeking verwijderd</b></p>";}
else {
echo "<p><b>Boeking niet verwijderd</b></p>";}
}
//UPDATE
if(isset($_POST['update_' . $reservationid]))
{
$NewStartDate = explode("-",$_POST[StartDate]);
$newstartdate = $NewStartDate[2] . "-" . $NewStartDate[1] . "-" . $NewStartDate[0];
$newstarttime = $_POST[StartTime] . ":00";
$newenddate = $newstartdate;
$NewEndTime = explode(":",$_POST[StopTime]);
$newendtime = mktime($NewEndTime[0],($NewEndTime[1]-1),59);
$newendtime = date("H:i:s",$newendtime);
$UpdateStart = $newstartdate . " " . $newstarttime;
$UpdateEnd = $newenddate . " " . $newendtime;
$UpdateUsername = $_POST[username];
$updatequery = "UPDATE reservations SET start='$UpdateStart', end='$UpdateEnd', username='$UpdateUsername' WHERE reservationid=" . $reservationid;
if(mysql_query($updatequery)){
echo "<p><b>Updated " . $reservationid . " " . $UpdateStart . " " . $UpdateEnd . " " . $UpdateUsername . "</b></p>";}
else {
echo "<p><b>FAILURE IS NOT AN OPTION. AGAIN!</b></p>";}
}
?>
<?php
}
mysql_close();
?>
The working code is:
<?php
//DELETE
if(isset($_POST['delete_' . $_POST[updateid]]))
{
$deletequery = "DELETE FROM reservations WHERE reservationid=" . $_POST[updateid];
if(mysql_query($deletequery)){
echo "<p><b>Boeking verwijderd</b></p>";
}
else {
echo "<p><b>FAILURE IS NOT AN OPTION. AGAIN!</b></p>";
}
}
//UPDATE
if(isset($_POST['update_' . $_POST[updateid]]))
{
$UpdateID = $_POST[updateid];
$NewStartDate = explode("-",$_POST[StartDate]);
$newstartdate = $NewStartDate[2] . "-" . $NewStartDate[1] . "-" . $NewStartDate[0];
$newstarttime = $_POST[StartTime] . ":00";
$newenddate = $newstartdate;
$NewEndTime = explode(":",$_POST[StopTime]);
$newendtime = mktime($NewEndTime[0],($NewEndTime[1]-1),59);
$newendtime = date("H:i:s",$newendtime);
$UpdateStart = $newstartdate . " " . $newstarttime;
$UpdateEnd = $newenddate . " " . $newendtime;
$UpdateUsername = $_POST[username];
$updatequery = "UPDATE reservations SET start='$UpdateStart', end='$UpdateEnd', username='$UpdateUsername' WHERE reservationid='$UpdateID'";
if(mysql_query($updatequery)){
echo "<p><b>Updated " . $reservationid . " " . $UpdateStart . " " . $UpdateEnd . " " . $UpdateUsername . "</b></p>";
}
else {
echo "<p><b>FAILURE IS NOT AN OPTION. AGAIN!</b></p>";
}
// echo "<p><b>Updated " . $reservationid . " " . $UpdateStart . " " . $UpdateEnd . " " . $UpdateUsername . "</b></p>";
}
?>
<?php
$query = "SELECT * FROM reservations WHERE (roomid = " . 45 . " AND end > NOW() ) ORDER BY start";
$result = mysql_query($query) or die(mysql_error());
?>
<table border="1">
<tr><td>Datum</td><td>Start</td><td>Stop</td><td>Gebruikersnaam</td></tr>
<?php
while($row = mysql_fetch_array($result)){
$reservationid=$row["reservationid"];
$username=$row["username"];
$aantal=$row["numberingroup"];
$reservationid=$row["reservationid"];
$start=$row["start"];
$end=$row["end"];
$roomid=$row["roomid"];
$startdate = explode(" ",$start);
$startdate[0] = explode("-",$startdate[0]);
$startdate[1] = explode(":",$startdate[1]);
$StartFormat = mktime($startdate[1][0],$startdate[1][1],$startdate[1][2],$startdate[0][1],$startdate[0][2],$startdate[0][0]);
$StartDate = date("d-m-Y",$StartFormat);
$StartTime = date("H:i",$StartFormat);
$stopdate = explode(" ",$end);
$stopdate[0] = explode("-",$stopdate[0]);
$stopdate[1] = explode(":",$stopdate[1]);
$StopFormat = mktime($stopdate[1][0],$stopdate[1][1],($stopdate[1][2]+1),$stopdate[0][1],$stopdate[0][2],$stopdate[0][0]);
$StopDate = date("d-m-Y",$StopFormat);
$StopTime = date("H:i",$StopFormat);
?>
<form action="" method="post">
<tr><td><input type="hidden" name="updateid" value="<?php echo $reservationid; ?>" /> <input name="StartDate" value="<? echo $StartDate; ?>" /></td><td><input name="StartTime" value="<? echo $StartTime; ?>" /></td><td><input name="StopTime" value="<? echo $StopTime; ?>" /></td><td><input name="username" value="<? echo $username;?>" /></td><td><input type="submit" value="update" name="<?php echo 'update_' . $reservationid; ?>" /></td><td> <input type="submit" value="delete" name="<?php echo 'delete_' . $reservationid; ?>" /></td> </tr>
</form>
<?php
}
mysql_close();
?>
</table>
Move the logic that does the updating and deleting above the logic that does the rendering:
<?php
// DELETE (your delete stuff)
// UPDATE (your update stuff)
// RETRIEVE (your SELECT query)
?>
<table> <!-- your table markup -->
<?php
// RENDER (your while loop and such)
You'll also need to adjust your logic a bit. You're using the $reservationid from the SELECT to do the deleting and updating. This doesn't work, because the execution context for the PHP is refreshed with each page load. What you need is to store the reservation id in each form (maybe in a hidden field), and then to retrieve that from $_POST.
Incidentally, your code is very vulnerable to SQL injection. Also, you should look at using mysqli or PDO; mysql_connect is deprecated in the current version of PHP.
You could use jQuery for this. You have to make an $.ajax (http://api.jquery.com/jquery.ajax/) call. From the callback you can fill/set the fields you want to. You'll need $('#idofelement').html()(http://api.jquery.com/html/) for this. If you have got any questions don't be affraid to ask ;) Good luck!

Unknown Column In Where Clause mysql query

My query is $query = "SELECT * FROM cartmatch WHERE CARTNO=$cart4"; and I'm receiving an error that says "Unknown column 'M833' in 'where clause'". Just so you know, cart4=M833.
::EDIT::
For some reason, nothing is showing. Here is the code on the page.
<?php
$cart1 = rawurldecode($_GET["path"]);
list( , , , , , $cart2) = explode ("\\", $cart1);
$cart3 = $cart2;
list($cart4) = explode (" ", $cart3);
$con = mysql_connect("SERVER","USERNAME","PASSWORD");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("cartmatch", $con);
$result = mysql_query("SELECT * FROM cartmatch WHERE CARTNO='$cart4'");
while($row = mysql_fetch_array($result))
{
echo '<form enctype="multipart/form-data" action="album.php" method="POST">Please enter press save.<br><br><input name="ID" type="hidden" value=';
echo $_GET["ID"];
echo ' ><input name="enabled" type="hidden" value=';
echo $_GET["enabled"];
echo ' ><input name="artist" type="hidden" value=';
echo $_GET["artist"];
echo ' ><input name="title" type="hidden" value="';
echo $_GET["title"];
echo '" >Name:<br/><input name="album" type="text" autofocus="autofocus" value="';
echo $row['ALBUM'];
echo '" ><input type="submit" name="edit" value="Save"></form>';
}
mysql_close($con);
?>
Change the query to:
"SELECT * FROM cartmatch WHERE CARTNO='$cart4'"
and change
list($cart4) = explode (" ", $cart3);
to
list($cart4) = explode ("+", $cart3);
Change the WHERE section to
CARTNO='$cart4'

PHP variables passed incorrectly to MySQL

I asked this question before, but not very well! Basically I have an editing page for a CMS, somewhere along the line (from the element onwards) the fields display in the box next to where they should be displaying. any ideas why?
<?php
if(isset($_GET['id']))
{
$query = "SELECT * ".
"FROM studies ".
"WHERE id = '".$_GET['id']."'";
$result = mysql_query($query) or die('Error : ' . mysql_error());
list($id, $pagetitle, $title, $date, $copy, $outputs, $strategies, $client, $niche, $media, $thumbmedia, $newfieldtitle, $newfieldcontent) = mysql_fetch_array($result, MYSQL_NUM);
}
if(isset($_POST['update1']))
{
$id = $_POST['id'];
$pagetitle = $_POST['pagetitle'];
$title = $_POST['title'];
$date = $_POST['date'];
$copy = $_POST['copy'];
$outputs = $_POST['outputs'];
$strategies = $_POST['strategies'];
$client = $_POST['client'];
$niche = $_POST['niche'];
$media = $_POST['media'];
$thumbmedia = $_POST['thumbmedia'];
$newfieldtitle = $_POST['newfieldtitle'];
$newfieldcontent = $_POST['newfieldcontent'];
if(!get_magic_quotes_gpc())
{
$pagetitle = addslashes($pagetitle);
$title = addslashes($title);
$date = addslashes($date);
$copy = addslashes($copy);
$outputs = addslashes($outputs);
$strategies = addslashes($strategies);
$client = addslashes($client);
$niche = addslashes($niche);
$media = addslashes($media);
$thumbmedia = addslashes($thumbmedia);
$newfieldtitle = addslashes($newfieldtitle);
$newfieldcontent = addslashes($newfieldcontent);
}
// update the article in the database
$query = "UPDATE studies
SET pagetitle = '$pagetitle', title = '$title', date = '$date', copy = '$copy', outputs = '$outputs', strategies = '$strategies', client = '$client', niche = '$niche', media = '$media', thumbmedia = '$thumbmedia', newfieldtitle = '$newfieldtitle', newfieldcontent = '$newfieldcontent' ".
"WHERE id = '$id'";
mysql_query($query) or die('Error : ' . mysql_error());
// then remove the cached file
$cacheDir = dirname(__FILE__) . '/cache/';
$cacheFile = $cacheDir . '_' . $_GET['id'] . '.html';
#unlink($cacheFile);
// and remove the index.html too because the file list
// is changed
#unlink($cacheDir . 'index.html');
echo "<b>Article '$title' updated</b>";
// now we will display $title & content
// so strip out any slashes
$pagetitle = stripslashes($pagetitle);
$title = stripslashes($title);
$date = stripslashes($date);
$copy = stripslashes($copy);
$outputs = stripslashes($outputs);
$strategies = stripslashes($strategies);
$client = stripslashes($client);
$niche = stripslashes($niche);
$media = stripslashes($media);
$thumbmedia = stripslashes($thumbmedia);
$newfieldtitle = stripslashes($newfieldtitle);
$newfieldcontent = stripslashes($newfieldcontent);
}
?>
<div class="container">
<form method="post">
<input type="hidden" name="id" value="<?php echo $id; ?>">
<p class="subheadsmall">Browser Title</p>
<textarea cols="40" rows="1" class="box" name="pagetitle" id="editbox"><?php echo $pagetitle; ?></textarea>
<p class="subheadsmall">Story Title</p>
<textarea cols="40" rows="1" class="box" name="title" id="editbox"><?php echo $title; ?></textarea>
<p class="subheadsmall">Date</p>
<textarea cols="40" rows="1" class="box" name="date" id="editbox"><?php echo $date; ?></textarea>
<p class="subheadsmall">Story</p>
<textarea cols="80" rows="10" class="box" name="copy" id="editbox"><?php echo $copy; ?></textarea>
<p class="subheadsmall">Outputs</p>
<textarea cols="80" rows="10" class="box" name="outputs" id="editbox"><?php echo $outputs; ?></textarea>
<p class="subheadsmall">Strategies</p>
<p class="subheadsmall">Client</p>
<select name="client">
<option value="empty">Select a Client...</option>
<?php
$result2 = mysql_query("SELECT name FROM clients");
if (!$result2) {
die("Database query failed: " . mysql_error());
}
while($row = mysql_fetch_array($result2)) {
$clientlist = $row['name'];
$clientname = htmlspecialchars($row['name']);
if ($_POST['client'] == $clientlist)
{
echo '<option value="' . $clientlist . '" selected="selected" >' . $clientname . '</option>' . '\n';
}
else{
echo '<option value="' . $clientlist . '" >' . $clientname . '</option>' . '\n';
}
}
?>
</select>
<p class="subheadsmall">Core Classification</p>
<?php
switch ($niche) {
case "brand":
echo '<input type="radio" name="niche" value="brand" checked="checked" />Brand';
echo '<input type="radio" name="niche" value="marketing" />Marketing';
echo '<input type="radio" name="niche" value="communication" />Communication';
break;
case "marketing":
echo '<input type="radio" name="niche" value="brand" />Brand';
echo '<input type="radio" name="niche" value="marketing" checked="checked" />Marketing';
echo '<input type="radio" name="niche" value="communication" />Communication';
break;
case "communication":
echo '<input type="radio" name="niche" value="brand" />Brand';
echo '<input type="radio" name="niche" value="marketing" />Marketing';
echo '<input type="radio" name="niche" value="communication" checked="checked" />Communication';
break;
default;
echo '<input type="radio" name="niche" value="brand" />Brand';
echo '<input type="radio" name="niche" value="marketing" />Marketing';
echo '<input type="radio" name="niche" value="communication" />Communication';
break;
}
?>
<p class="subheadsmall">Add New Strategy</p>
<textarea cols="40" rows="1" class="box" name="strategies" id="editbox"><?php echo $strategies; ?></textarea>
<p class="subheadsmall">Media</p>
<textarea cols="80" rows="10" class="box" name="media" id="editbox"><?php echo $media; ?></textarea>
<p class="subheadsmall">Thumbnail image</p>
<textarea cols="80" rows="3" class="box" name="thumbmedia" id="editbox"><?php echo $thumbmedia; ?></textarea>
<p class="subheadsmall">Additional Field</p>
<p class="subheadsmall">Additional Field Title</p>
<textarea cols="40" rows="1" class="box" name="newfieldtitle" id="editbox"><?php echo $newfieldtitle; ?></textarea>
<p class="subheadsmall">Additional Field Content</p>
<textarea cols="40" rows="3" class="box" name="newfieldcontent" id="editbox"><?php echo $newfieldcontent; ?></textarea>
<input name="update1" type="submit" class="box" id="editbutton" value="Update Article">
</form>
A side note about security :
Please, for the sake of the internet and all your users, don't use mysql_query. Please use PDO http://php.net/pdo. It automatically escapes your variables so you don't have SQL exploits.
And if you must use mysql_query (for legacy code) make sure to run each variable through http://php.net/mysql_real_escape_string before using it in a query string.
I suppose you're simply assigning the wrong content to the wrong variables, which supposedly happens here:
list($id, $pagetitle, $title, ...) = mysql_fetch_array($result, MYSQL_NUM);
You're relying on the database fields being in the exact order your code is in. Not very reliable and a horror to maintain.
Why go through the trouble of copying them out of an array into separate variables in the first place? Just keep them as they are until you need them:
<?php $row = mysql_fetch_assoc($result); ?>
...
<textarea name="date"><?php echo $row['date']; ?></textarea>
Remove the addslashes and magic quotes crap replace it with mysql_real_escape_string()
You are leaving yourself open to sql injections with the SELECT * FROM studies WHERE id = '".$_GET['id']."'";
What if I make a request like: domain.tld/page.ext?id=SELECT * FROM users
I've rewritten a bunch of the problems I saw give this a try.
<?php
if(isset($_GET['id']))
{
$query = "SELECT * FROM studies WHERE id = " . mysql_real_escape_string($_GET['id']);
$result = mysql_query($query) or die('Error : ' . mysql_error());
list($id, $pagetitle, $title, $date, $copy, $outputs, $strategies, $client, $niche, $media, $thumbmedia, $newfieldtitle, $newfieldcontent) = mysql_fetch_array($result, MYSQL_NUM);
}
if(isset($_POST['update1']))
{
$id = $_POST['id'];
$pagetitle = $_POST['pagetitle'];
$title = $_POST['title'];
$date = $_POST['date'];
$copy = $_POST['copy'];
$outputs = $_POST['outputs'];
$strategies = $_POST['strategies'];
$client = $_POST['client'];
$niche = $_POST['niche'];
$media = $_POST['media'];
$thumbmedia = $_POST['thumbmedia'];
$newfieldtitle = $_POST['newfieldtitle'];
$newfieldcontent = $_POST['newfieldcontent'];
// update the article in the database
$query = "UPDATE studies
SET pagetitle = '" . mysql_real_escape_string($pagetitle) . "', title = '" . mysql_real_escape_string($title) . "', date = '" . mysql_real_escape_string($date) . "', copy = '" . mysql_real_escape_string($copy) . "', outputs = '" . mysql_real_escape_string($outputs) . "', strategies = '" . mysql_real_escape_string($strategies) . "', client = '" . mysql_real_escape_string($client) . "', niche = '" . mysql_real_escape_string($niche) . "', media = '" . mysql_real_escape_string($media) . "', thumbmedia = '" . mysql_real_escape_string($thumbmedia) . "', newfieldtitle = '" . mysql_real_escape_string($newfieldtitle) . "', newfieldcontent = '" . mysql_real_escape_string($newfieldcontent) . "' ".
"WHERE id = '" . mysql_real_escape_string($id) . "'";
mysql_query($query) or die('Error : ' . mysql_error());
// then remove the cached file
$cacheDir = dirname(__FILE__) . '/cache/';
$cacheFile = $cacheDir . '_' . $_GET['id'] . '.html';
#unlink($cacheFile);
// and remove the index.html too because the file list
// is changed
#unlink($cacheDir . 'index.html');
echo "<b>Article '$title' updated</b>";
}
?>
<div class="container">
<form method="post">
<input type="hidden" name="id" value="<?php echo $id; ?>">
<p class="subheadsmall">Browser Title</p>
<textarea cols="40" rows="1" class="box" name="pagetitle" id="editbox"><?php echo $pagetitle; ?></textarea>
<p class="subheadsmall">Story Title</p>
<textarea cols="40" rows="1" class="box" name="title" id="editbox"><?php echo $title; ?></textarea>
<p class="subheadsmall">Date</p>
<textarea cols="40" rows="1" class="box" name="date" id="editbox"><?php echo $date; ?></textarea>
<p class="subheadsmall">Story</p>
<textarea cols="80" rows="10" class="box" name="copy" id="editbox"><?php echo $copy; ?></textarea>
<p class="subheadsmall">Outputs</p>
<textarea cols="80" rows="10" class="box" name="outputs" id="editbox"><?php echo $outputs; ?></textarea>
<p class="subheadsmall">Strategies</p>
<p class="subheadsmall">Client</p>
<select name="client">
<option value="empty">Select a Client...</option>
<?php
$result2 = mysql_query("SELECT name FROM clients") or die("Database query failed: " . mysql_error());
while($row = mysql_fetch_assoc($result2)) {
$clientlist = $row['name'];
$clientname = htmlspecialchars($row['name']);
if ($_POST['client'] == $clientlist)
{
echo '<option value="' . $clientlist . '" selected="selected" >' . $clientname . '</option>' . '\n';
}
else{
echo '<option value="' . $clientlist . '" >' . $clientname . '</option>' . '\n';
}
}
?>
</select>
<p class="subheadsmall">Core Classification</p>
<?php
switch ($niche) {
case "brand":
echo '<input type="radio" name="niche" value="brand" checked="checked" />Brand';
echo '<input type="radio" name="niche" value="marketing" />Marketing';
echo '<input type="radio" name="niche" value="communication" />Communication';
break;
case "marketing":
echo '<input type="radio" name="niche" value="brand" />Brand';
echo '<input type="radio" name="niche" value="marketing" checked="checked" />Marketing';
echo '<input type="radio" name="niche" value="communication" />Communication';
break;
case "communication":
echo '<input type="radio" name="niche" value="brand" />Brand';
echo '<input type="radio" name="niche" value="marketing" />Marketing';
echo '<input type="radio" name="niche" value="communication" checked="checked" />Communication';
break;
default;
echo '<input type="radio" name="niche" value="brand" />Brand';
echo '<input type="radio" name="niche" value="marketing" />Marketing';
echo '<input type="radio" name="niche" value="communication" />Communication';
break;
}
?>
<p class="subheadsmall">Add New Strategy</p>
<textarea cols="40" rows="1" class="box" name="strategies" id="editbox"><?php echo $strategies; ?></textarea>
<p class="subheadsmall">Media</p>
<textarea cols="80" rows="10" class="box" name="media" id="editbox"><?php echo $media; ?></textarea>
<p class="subheadsmall">Thumbnail image</p>
<textarea cols="80" rows="3" class="box" name="thumbmedia" id="editbox"><?php echo $thumbmedia; ?></textarea>
<p class="subheadsmall">Additional Field</p>
<p class="subheadsmall">Additional Field Title</p>
<textarea cols="40" rows="1" class="box" name="newfieldtitle" id="editbox"><?php echo $newfieldtitle; ?></textarea>
<p class="subheadsmall">Additional Field Content</p>
<textarea cols="40" rows="3" class="box" name="newfieldcontent" id="editbox"><?php echo $newfieldcontent; ?></textarea>
<input name="update1" type="submit" class="box" id="editbutton" value="Update Article">
</form>
EDIT: I've made a few more changes to your code, also I think your problem stems from this line:
while($row = mysql_fetch_array($result2)) {
I think your looking for the mysql_fetch_assoc() array.

Categories