Jump to content

Problem with Update function in php script


a_tzounis

Recommended Posts

Hello everybody! Recently I ve been working on a php script that manages entries from a SQL DB using the "Update" function.
Until now i can demonstrate the entry i have chosen to edit, by filling text fields just like the ones i used to insert an entry to my DB. What i cannot do is to UPDATE these entries in the DB, although the script informs me that update was successful. Any ideas?
Thanks for taking time to read my post anyway.
A.


The script bellow is the malfunctioning script i m talking about...

[code]
<?php
  session_start();
  // check session variable
  if (session_is_registered("valid_user")) {  // The aministrator has previously logged in
  session_register("page");
$page = "page1";
?>
<?php
include("connect_to_db.php");
?>
<html>
<head>
<title>Edit Entries page. Connected user: <? echo $valid_user . " @ " . date("l, F j Y"); ?> </title>
<style type="text/css">
<!--
.style1 {
font-size: large;
color: #3458F6;
font-family: Arial, Helvetica, sans-serif;
}
body,td,th {
font-size: 12px;
color: #3458F6;
font-family: Arial, Helvetica, sans-serif;
}
a:link {
color: #76B075;
}
a:visited {
color: #75AF74;
}
a:hover {
color: #2859DC;
}
a:active {
color: #76B075;
}
.style2 {
font-size: 16px;
color: #325ADE;
font-weight: bold;
}
.style4 {color: #325ADE; font-size: 12px;}
.style7 {
font-size: 14px;
font-weight: bold;
color: #75AF74;
}
a {
font-family: Arial, Helvetica, sans-serif;
}
h1,h2,h3,h4,h5,h6 {
font-family: Arial, Helvetica, sans-serif;
}
h3 {
font-size: 14px;
color: #325ADE;
}
.style10 {color: #FFFFFF}
.style11 {
font-size: 14px;
font-weight: bold;
}
.style12 {
font-size: 24px;
color: #FF0000;
font-family: "Times New Roman", Times, serif;
font-weight: bold;
}
.style13 {font-size: 12px}
body {
margin-top: 0px;
margin-left: 5px;
}
#Layer1 {
position:absolute;
left:5px;
top:157px;
width:799px;
height:209px;
z-index:1;
}
.style14 {color: #FF0000}
-->
</style>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1253"></head>
<body>
<div id="Layer1">
  <p class="style2">Choose the fields you want to edit. </p>
  <p class="style4">
    <span class="style12">i: </span>
<?php
//include("connect_to_db.php");
//$query = "SELECT * FROM journal_publications where id=$id";
//$result = mysql_query($query);
//ifset($edit);
//$result = mysql_query($query);
$id=mysql_query("select id from journal_publications where id=$edit");
$authors=mysql_query("select authors from journal_publications where id=$edit");
$title=mysql_query("select title from journal_publications where id=$edit");
$state=mysql_query("select state from journal_publications where id=$edit");
$place=mysql_query("select place from journal_publications where id=$edit");
$userfile=mysql_query("select userfile from journal_publications where id=$edit");
if($_POST['submit'])
{
$sql = mysql_query("update journal_publications set authors='$authors' ,title='$title', state='$state', place='$place', userfile='$userfile' where id='$edit'");
//echo "$sql";
$result = mysql_query($sql);
echo "The fields you have chosen have successfully been modified.";
echo "<p><a href=\"upload.php\">Return to New Entry page.</a>";
echo "<p><a href=\"edit_news.php\">Return to the Edit Entries page.</a>";
echo "<p><a href=\"user_login.php\">Return to Main Menu.</a>";
echo "<p>$sql";
echo "<p>$result";
//echo "<p>$edit";
}
else if(!$sql)
{
echo "There was no change in your data base!";
?>
  </p>
  <form name="editform" action="<?php echo $PHP_SELF ?>" method="post" enctype="multipart/form-data">
    <p class="style11">1. Edit your Journal Publications. </p>
    <p class="style7">
        <?php
      $id=mysql_fetch_array($id);
      $id=$id['id'];
          ?>
      <input type="hidden" name="id" value="<?php echo $id; ?>">
    </p>
    <table width="541" border="0">
      <tr>
        <td width="148">Authors:</td>
        <td width="277">
<?php
      $authors=mysql_fetch_array($authors);
      $authors=$authors['authors'];
          ?>
  <input name="authors" type="text" id="authors" value="<?php echo $authors; ?> "size="40" maxlength="255"></td>
      </tr>
      <tr>
        <td>Title:<span class="style10">.</span></td>
        <td>
<?php
      $title=mysql_fetch_array($title);
      $title=$title['title'];
          ?>
<input name="title" type="text" id="title" value="<?php echo $title; ?>" size="40" maxlength="128"></td>
      </tr>
      <tr>
        <td>To appear/ Submitted :</td>
        <td>
<select name="state" id="state">
<?php
      $state=mysql_fetch_array($state);
      $state=$state['state'];
          ?>
            <option value=""><?php echo $state; ?></option>
            <option value="to appear">to appear</option>
            <option value="submitted to">submitted to</option>
            <option value="published in">published in</option>
        </select></td>
      </tr>
      <tr>
        <td>Where?:<span class="style10">.</span></td>
        <td>
<?php
      $place=mysql_fetch_array($place);
      $place=$place['place'];
          ?>
<input name="place" type="text" id="place" value="<?php echo $place; ?>" size="40" maxlength="128"></td>
      </tr>
      <tr>
        <td>Recent file:</td>
        <td><?php
      $userfile=mysql_fetch_array($userfile);
      $userfile=$userfile['userfile'];
          ?>
          <input name="userfile_name" type="text" id="userfile_name" value="<?php echo $userfile; ?>" size="40" maxlength="255">          </td>
      </tr>
      <tr>
        <td><span class="style14">New archive:</span></td>
        <td>
  <input type= "hidden" name="MAX_FILE_SIZE" value="5000000">
        <input name="userfile" type="file" id="userfile" value="" size="40"></td>
      </tr>
    </table>
    <p>
      <input name="submit" id="submit" type="submit" value="Update">
    </p>
  </form>
  <p>&nbsp; </p>
  </form>
  <? echo "<p>$authors";
echo "<p>$id";
echo "<p>$edit";
?>
  <p class="style7">&nbsp;</p>
  <p><span class="style13">[ <a href="user_login.php" target="_self">Return to main menu.</a> ]</span></p>
  <p><span class="style13">[ <a href="logout_users.php">Disconnect</a> ] </span>
      <?php
  } else {  // Somebody tried to access this page without previously logging in
    echo "<h3>You cannot view this page. Please login.</h3>";
echo "<br><a href=\"user_login.php\">Return to log-in page. </a>";
  }
?>
  </p>
</div>

<p class="style1"><img src="header_cp.jpg" alt="cp" width="800" height="154"></p>
<p class="style2">&nbsp;</p>
</body>
<?php
}
?>
</html>
[/code]
Try echo'ing mysql_error(); immediately after the SQL call.  It should tell you what, if anything, has gone wrong.

I'll often get the script to echo the $sql line then copy + paste it into phpMyAdmin or something similar to see if it works there - it tends to be more descriptive on errors.
Thank you for your comments!!! It still doesn't work. The thing is that i have already checked the
$sql query in PhpMyAdmin and works fine. Moreover i use this "if" so to demonstrate the progress of the update and what i get as a result is that the fileds i have chosen to edit where successfully edited. I am stuck to this for over a week now. I don' t really know what goes wrong...  >:(
my friend, you are calling the variables from the database 2 lines before u try update them at
[code]
<?php
//include("connect_to_db.php");
//$query = "SELECT * FROM journal_publications where id=$id";
//$result = mysql_query($query);
//ifset($edit);
//$result = mysql_query($query);
$id=mysql_query("select id from journal_publications where id=$edit");
$authors=mysql_query("select authors from journal_publications where id=$edit");
$title=mysql_query("select title from journal_publications where id=$edit");
$state=mysql_query("select state from journal_publications where id=$edit");
$place=mysql_query("select place from journal_publications where id=$edit");
$userfile=mysql_query("select userfile from journal_publications where id=$edit");
if($_POST['submit'])
{
$sql = mysql_query("update journal_publications set authors='$authors' ,title='$title', state='$state', place='$place', userfile='$userfile' where id='$edit'");
//echo "$sql";
$result = mysql_query($sql);
[/code]
try changing the variables in the query from lets say $authors to $_POST[authors] and it should work...
Till now i have made it to work... The only problem is that in this code i also have to update a file as well, which means that in case we have a new file (userfile1) i sholud store/update the new userfile1_name plus the new userfile1 file - whatever this can be - to my DB and the filesystem as well as to unlink the old file, ELSE leave it as it is in both the name record in the DB and the filesystem of my server (where the uploaded files are). Any ideas about how i could translate my thought in PHP code???
Check my code till now...
[code]<?php
  session_start();
  // check session variable
  if (session_is_registered("valid_user")) {   // The aministrator has previously logged in
  session_register("page");
$page = "page1";
?>
<?php
include("connect_to_db.php");
?>
<html>
<head>
<title>Update page. User: <? echo $valid_user . " @ " . date("l, F j Y"); ?> </title>
<style type="text/css">
<!--
.style1 {
font-size: large;
color: #3458F6;
font-family: Arial, Helvetica, sans-serif;
}
body,td,th {
font-size: 12px;
color: #3458F6;
font-family: Arial, Helvetica, sans-serif;
}
a:link {
color: #76B075;
}
a:visited {
color: #75AF74;
}
a:hover {
color: #2859DC;
}
a:active {
color: #76B075;
}
.style2 {
font-size: 16px;
color: #325ADE;
font-weight: bold;
}
.style4 {color: #325ADE; font-size: 12px;}
.style7 {
font-size: 14px;
font-weight: bold;
color: #75AF74;
}
a {
font-family: Arial, Helvetica, sans-serif;
}
h1,h2,h3,h4,h5,h6 {
font-family: Arial, Helvetica, sans-serif;
}
h3 {
font-size: 14px;
color: #325ADE;
}
.style10 {color: #FFFFFF}
.style11 {
font-size: 14px;
font-weight: bold;
}
.style12 {
font-size: 24px;
color: #FF0000;
font-family: "Times New Roman", Times, serif;
font-weight: bold;
}
.style13 {font-size: 12px}
body {
margin-top: 0px;
margin-left: 5px;
}
#Layer1 {
position:absolute;
left:5px;
top:157px;
width:799px;
height:209px;
z-index:1;
}
.style14 {color: #FF0000}
-->
</style>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1253"></head>
<body>
<div id="Layer1">
  <p class="style2">Choose the fields you want to edit. </p>
  <p class="style4">
    <!-- Na allaxei to destination page edo!!!!-->
    <span class="style12">i: </span>
<?php
$id=mysql_query("select id from journal_publications where id=$edit");
     //$id=mysql_fetch_array($id);
   //  $id=$id['id'];

$authors=mysql_query("select authors from journal_publications where id=$edit");
      $authors=mysql_fetch_array($authors);
      $authors=$authors['authors'];
$title=mysql_query("select title from journal_publications where id=$edit");
$title=mysql_fetch_array($title);
      $title=$title['title'];
$state=mysql_query("select state from journal_publications where id=$edit");
   $state=mysql_fetch_array($state);
      $state=$state['state'];
$place=mysql_query("select place from journal_publications where id=$edit");
  $place=mysql_fetch_array($place);
      $place=$place['place'];
$userfile=mysql_query("select userfile from journal_publications where id=$edit");
    $userfile=mysql_fetch_array($userfile);
      $userfile=$userfile['userfile'];
if($_POST['submit'])
{
$sql = "update journal_publications set authors='$authors1' ,title='$title1', state='$state1', place='$place1', userfile='$userfile1_name' where id='$edit'";
//echo "$sql";
$id=mysql_query("select id from journal_publications where id=$edit");
      //$id=mysql_fetch_array($id);
     // $id=$id['id'];
//echo "$id";
$authors=mysql_query("select authors from journal_publications where id=$edit");
      $authors=mysql_fetch_array($authors);
      $authors=$authors['authors'];
$title=mysql_query("select title from journal_publications where id=$edit");
$title=mysql_fetch_array($title);
      $title=$title['title'];
$state=mysql_query("select state from journal_publications where id=$edit");
   $state=mysql_fetch_array($state);
      $state=$state['state'];
$place=mysql_query("select place from journal_publications where id=$edit");
  $place=mysql_fetch_array($place);
      $place=$place['place'];
$userfile=mysql_query("select userfile from journal_publications where id=$edit");
    $userfile=mysql_fetch_array($userfile);
      $userfile=$userfile['userfile'];
$result = mysql_query($sql);
echo "Fields successfuly updated.";
echo "<p><a href=\"upload.php\">Return to page1.</a>";
echo "<p><a href=\"edit_news.php\">Return to page2.</a>";
echo "<p><a href=\"user_login.php\">Return to page3</a>";
echo "<p>$sql";
echo "<p>$result";
echo "<p>$sql";
echo "to edit einai:";
echo "<p>$edit";
//echo "<p>$edit";
}
else if(!$sql)
{
echo "No changes have been made to your DB!";
?>
  </p>
  <form name="editform" action="admin_edit_new.php?edit=<?php echo "$edit" ?>" method="post" enctype="multipart/form-data">
    <p class="style11">1. Edit Journal Publications. </p>
    <p class="style7">
        <?php
      $edit=$id;
  echo "$edit";
           ?>
      <input type="hidden" name="id" value="<?php echo $id; ?>">
    </p>
    <table width="541" border="0">
      <tr>
        <td width="148">Authors:</td>
        <td width="277">
   <input name="authors1" type="text" id="authors1" value="<?php echo $authors; ?> "size="40" maxlength="255"></td>
      </tr>
      <tr>
        <td>Τίτλος:<span class="style10">.</span></td>
        <td>
<input name="title1" type="text" id="title1" value="<?php echo $title; ?>" size="40" maxlength="128"></td>
      </tr>
      <tr>
        <td>To appear/ Submitted :</td>
        <td>
<select name="state1" id="state1">
        <option value="<?php echo $state; ?>"><?php echo $state; ?></option>
            <option value="to appear">to appear</option>
            <option value="submitted to">submitted to</option>
            <option value="published in">published in</option>
        </select></td>
      </tr>
      <tr>
        <td>Where?:<span class="style10">.</span></td>
        <td>
<input name="place1" type="text" id="place1" value="<?php echo $place; ?>" size="40" maxlength="128"></td>
      </tr>
      <tr>
        <td>Recent file:</td>
        <td>
          <input name="userfile_name" type="text" id="userfile_name" value="<?php echo $userfile; ?>" size="40" maxlength="255">          </td>
      </tr>
      <tr>
        <td><span class="style14">New file:</span></td>
        <td>
   <input type= "hidden" name="MAX_FILE_SIZE" value="5000000">
        <input name="userfile1" type="file" id="userfile1" value="" size="40"></td>
      </tr>
    </table>
    <p>
      <input name="submit" id="submit" type="submit" value="Update">
    </p>
  </form>
  <p>&nbsp; </p>
  </form>
  <p class="style7">&nbsp;</p>
  <p><span class="style13">[ <a href="user_login.php" target="_self">Return to main menu</a> ]</span></p>
  <p><span class="style13">[ <a href="logout_users.php">Disconnect</a> ] </span>
      <?php
  } else {   // Somebody tried to access this page without previously logging in
    echo "<h3>View page only by registered users.</h3>";
echo "<br><a href=\"user_login.php\">Return to intro page. </a>";
  }
?>
  </p>
</div>

<p class="style1"><img src="header_cp.jpg" alt="cp" width="800" height="154"></p>
<p class="style2">&nbsp;</p>
</body>
<?php
}
?>
</html>[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.