Jump to content

Fields empty after update, keep fields from changing


Recommended Posts

I´ve made a add/edit/insert row form to mysql with file upload using the software "PHP Generator for MySQL" but since it lacks saving file name, type and size I've been trying to add that to the code. I've succeeded almost. My problem is when it comes to updating text fields but keeping file as it is. When I do that the file name, type and size gets erased.

 

Serverversion: 5.0.32-Debian_7etch8-log

 

Table structure:

CREATE TABLE `konferenser` (

  `id` int(11) NOT NULL AUTO_INCREMENT,

  `datum` varchar(11) NOT NULL,

  `streck` varchar(3) DEFAULT NULL,

  `datum2` varchar(11) DEFAULT NULL,

  `aktivitet` varchar(255) NOT NULL,

  `weblink` varchar(255) DEFAULT NULL,

  `name` varchar(255) DEFAULT NULL,

  `type` varchar(50) DEFAULT NULL,

  `size` bigint(20) unsigned DEFAULT NULL,

  `content` mediumblob,

  PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1;

 

And the code:

 

<?php session_start();

?>

<html>
<head>
<title>shte_eu -- konferenser</title>
<meta name="generator" http-equiv="content-type" content="text/html">

</head>
<body>

<?php
  $conn = connect();
  $showrecs = 20;
  $pagerange = 10;

  $a = @$_GET["a"];
  $recid = @$_GET["recid"];
  $page = @$_GET["page"];
  if (!isset($page)) $page = 1;

  $sql = @$_POST["sql"];

  switch ($sql) {
    case "insert":
      sql_insert();
      break;
    case "update":
      sql_update();
      break;
    case "delete":
      sql_delete();
      break;
  }

  switch ($a) {
    case "add":
      addrec();
      break;
    case "view":
      viewrec($recid);
      break;
    case "edit":
      editrec($recid);
      break;
    case "del":
      deleterec($recid);
      break;
    default:
      select();
      break;
  }


  mysql_close($conn);
?>

</body>
</html>

<?php function select()
  {
  global $a;
  global $showrecs;
  global $page;

  $res = sql_select();
  $count = sql_getrecordcount();
  if ($count % $showrecs != 0) {
    $pagecount = intval($count / $showrecs) + 1;
  }
  else {
    $pagecount = intval($count / $showrecs);
  }
  $startrec = $showrecs * ($page - 1);
  if ($startrec < $count) {mysql_data_seek($res, $startrec);}
  $reccount = min($showrecs * $page, $count);
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr><td>Table: konferenser</td></tr>
<tr><td>Records shown <?php echo $startrec + 1 ?> - <?php echo $reccount ?> of <?php echo $count ?></td></tr>
</table>
<hr size="1" noshade>
<br>
<table class="tbl" border="0" cellspacing="1" cellpadding="5"width="100%">
<tr>
<td class="hr"> </td>
<td class="hr"> </td>
<td class="hr"> </td>
<td class="hr"><?php echo "datum" ?></td>
<td class="hr"><?php echo "streck" ?></td>
<td class="hr"><?php echo "datum2" ?></td>
<td class="hr"><?php echo "aktivitet" ?></td>
<td class="hr"><?php echo "weblink" ?></td>
<td class="hr"><?php echo "name" ?></td>
</tr>
<?php
  for ($i = $startrec; $i < $reccount; $i++)
  {
    $row = mysql_fetch_assoc($res);
    $style = "dr";
    if ($i % 2 != 0) {
      $style = "sr";
    }
?>
<tr>
<td class="<?php echo $style ?>"><a href="konferenser.php?a=view&recid=<?php echo $i ?>">View</a></td>
<td class="<?php echo $style ?>"><a href="konferenser.php?a=edit&recid=<?php echo $i ?>">Edit</a></td>
<td class="<?php echo $style ?>"><a href="konferenser.php?a=del&recid=<?php echo $i ?>">Delete</a></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["datum"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["streck"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["datum2"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["aktivitet"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["weblink"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["name"]) ?></td>
</tr>
<?php
  }
  mysql_free_result($res);
?>
</table>
<br>
<?php showpagenav($page, $pagecount); ?>
<?php } ?>

<?php function showrow($row, $recid)
  {
?>
<table class="tbl" border="0" cellspacing="1" cellpadding="5"width="50%">
<tr>
<td class="hr"><?php echo htmlspecialchars("datum")." " ?></td>
<td class="dr"><?php echo htmlspecialchars($row["datum"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("streck")." " ?></td>
<td class="dr"><?php echo htmlspecialchars($row["streck"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("datum2")." " ?></td>
<td class="dr"><?php echo htmlspecialchars($row["datum2"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("aktivitet")." " ?></td>
<td class="dr"><?php echo htmlspecialchars($row["aktivitet"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("weblink")." " ?></td>
<td class="dr"><?php echo htmlspecialchars($row["weblink"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("name")." " ?></td>
<td class="dr"><?php echo htmlspecialchars($row["name"]) ?></td>
</tr>
</table>
<?php } ?>

<?php function showroweditor($row, $iseditmode)
  {
  global $conn;
?>
<table class="tbl" border="0" cellspacing="1" cellpadding="5"width="50%">
<tr>
<td class="hr"><?php echo htmlspecialchars("datum")." " ?></td>
<td class="dr"><input type="text" name="datum" value="<?php echo str_replace('"', '"', trim($row["datum"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("streck")." " ?></td>
<td class="dr"><input type="text" name="streck" maxlength="11" value="<?php echo str_replace('"', '"', trim($row["streck"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("datum2")." " ?></td>
<td class="dr"><input type="text" name="datum2" maxlength="3" value="<?php echo str_replace('"', '"', trim($row["datum2"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("aktivitet")." " ?></td>
<td class="dr"><textarea cols="35" rows="4" name="aktivitet" maxlength="11"><?php echo str_replace('"', '"', trim($row["aktivitet"])) ?></textarea></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("weblink")." " ?></td>
<td class="dr"><textarea cols="35" rows="4" name="weblink" maxlength="255"><?php echo str_replace('"', '"', trim($row["weblink"])) ?></textarea></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("content")." " ?></td>
<td class="dr">
<?php if ($iseditmode)
{
?> <input type="radio" name="a_content" id="a_content" value="1" checked>Keep 
<input type="radio" name="a_content" id="a_content" value="2">Remove 
<input type="radio" name="a_content" id="a_content" value="3">Replace<br>
<?php }
?> <input type="file" name="x_content" id="x_content" size="30" onChange="if (this.form.a_content[2]) this.form.a_content[2].checked=true;" >
</td>
</tr>
</table>
<?php } ?>

<?php function showpagenav($page, $pagecount)
{
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="konferenser.php?a=add">Add Record</a> </td>
<?php if ($page > 1) { ?>
<td><a href="konferenser.php?page=<?php echo $page - 1 ?>"><< Prev</a> </td>
<?php } ?>
<?php
  global $pagerange;

  if ($pagecount > 1) {

  if ($pagecount % $pagerange != 0) {
    $rangecount = intval($pagecount / $pagerange) + 1;
  }
  else {
    $rangecount = intval($pagecount / $pagerange);
  }
  for ($i = 1; $i < $rangecount + 1; $i++) {
    $startpage = (($i - 1) * $pagerange) + 1;
    $count = min($i * $pagerange, $pagecount);

    if ((($page >= $startpage) && ($page <= ($i * $pagerange)))) {
      for ($j = $startpage; $j < $count + 1; $j++) {
        if ($j == $page) {
?>
<td><b><?php echo $j ?></b></td>
<?php } else { ?>
<td><a href="konferenser.php?page=<?php echo $j ?>"><?php echo $j ?></a></td>
<?php } } } else { ?>
<td><a href="konferenser.php?page=<?php echo $startpage ?>"><?php echo $startpage ."..." .$count ?></a></td>
<?php } } } ?>
<?php if ($page < $pagecount) { ?>
<td> <a href="konferenser.php?page=<?php echo $page + 1 ?>">Next >></a> </td>
<?php } ?>
</tr>
</table>
<?php } ?>

<?php function showrecnav($a, $recid, $count)
{
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="konferenser.php">Index Page</a></td>
<?php if ($recid > 0) { ?>
<td><a href="konferenser.php?a=<?php echo $a ?>&recid=<?php echo $recid - 1 ?>">Prior Record</a></td>
<?php } if ($recid < $count - 1) { ?>
<td><a href="konferenser.php?a=<?php echo $a ?>&recid=<?php echo $recid + 1 ?>">Next Record</a></td>
<?php } ?>
</tr>
</table>
<hr size="1" noshade>
<?php } ?>

<?php function addrec()
{
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="konferenser.php">Index Page</a></td>
</tr>
</table>
<hr size="1" noshade>
<form enctype="multipart/form-data" action="konferenser.php" method="post">
<p><input type="hidden" name="sql" value="insert"></p>
<?php
$row = array(
  "id" => "",
  "datum" => "",
  "streck" => "",
  "datum2" => "",
  "aktivitet" => "",
  "weblink" => "",
  "name" => "",
  "type" => "",
  "size" => "");
showroweditor($row, false);
?>
<p><input type="submit" name="action" value="Post"></p>
</form>
<?php } ?>

<?php function viewrec($recid)
{
  $res = sql_select();
  $count = sql_getrecordcount();
  mysql_data_seek($res, $recid);
  $row = mysql_fetch_assoc($res);
  showrecnav("view", $recid, $count);
?>
<br>
<?php showrow($row, $recid) ?>
<br>
<hr size="1" noshade>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="konferenser.php?a=add">Add Record</a></td>
<td><a href="konferenser.php?a=edit&recid=<?php echo $recid ?>">Edit Record</a></td>
<td><a href="konferenser.php?a=del&recid=<?php echo $recid ?>">Delete Record</a></td>
</tr>
</table>
<?php
  mysql_free_result($res);
} ?>

<?php function editrec($recid)
{
  $res = sql_select();
  $count = sql_getrecordcount();
  mysql_data_seek($res, $recid);
  $row = mysql_fetch_assoc($res);
  showrecnav("edit", $recid, $count);
?>
<br>
<form enctype="multipart/form-data" action="konferenser.php" method="post">
<input type="hidden" name="sql" value="update">
<input type="hidden" name="xid" value="<?php echo $row["id"] ?>">
<?php showroweditor($row, true); ?>
<p><input type="submit" name="action" value="Post"></p>
</form>
<?php
  mysql_free_result($res);
} ?>

<?php function deleterec($recid)
{
  $res = sql_select();
  $count = sql_getrecordcount();
  mysql_data_seek($res, $recid);
  $row = mysql_fetch_assoc($res);
  showrecnav("del", $recid, $count);
?>
<br>
<form action="konferenser.php" method="post">
<input type="hidden" name="sql" value="delete">
<input type="hidden" name="xid" value="<?php echo $row["id"] ?>">
<?php showrow($row, $recid) ?>
<p><input type="submit" name="action" value="Confirm"></p>
</form>
<?php
  mysql_free_result($res);
} ?>

<?php function connect()
{
  $conn = mysql_connect("localhost", "username", "password");
  mysql_select_db("shte_eu");
  return $conn;
}

function sqlvalue($val, $quote)
{
  if ($quote)
    $tmp = sqlstr($val);
  else
    $tmp = $val;
  if ($tmp == "")
    $tmp = "NULL";
  elseif ($quote)
    $tmp = "'".$tmp."'";
  return $tmp;
}

function sqlstr($val)
{
  return str_replace("'", "''", $val);
}

function sql_select()
{
  global $conn;
  $sql = "SELECT `id`, `datum`, `streck`, `datum2`, `aktivitet`, `weblink`, `name`, `type`, `size`, `content` FROM `konferenser`";
  $res = mysql_query($sql, $conn) or die(mysql_error());
  return $res;
}

function sql_getrecordcount()
{
  global $conn;
  $sql = "SELECT COUNT(*) FROM `konferenser`";
  $res = mysql_query($sql, $conn) or die(mysql_error());
  $row = mysql_fetch_assoc($res);
  reset($row);
  return current($row);
}

function sql_insert()
{
  global $conn;
  global $_POST;

  $sql = "insert into `konferenser` (`datum`, `streck`, `datum2`, `aktivitet`, `weblink`, `name`, `type`, `size`, `content`) values (" .sqlvalue(@$_POST["datum"], true).", " .sqlvalue(@$_POST["streck"], true).", " .sqlvalue(@$_POST["datum2"], true).", " .sqlvalue(@$_POST["aktivitet"], true).", " .sqlvalue(@$_POST["weblink"], true).", ".sqlvalue(@$_FILES["x_content"]["name"], true).", ".sqlvalue(@$_FILES["x_content"]["type"], true).", ".sqlvalue(@$_FILES["x_content"]["size"], true).", " .get_file_content_as_sql("content") .")";
  mysql_query($sql, $conn) or die(mysql_error());
}

function sql_update()
{
  global $conn;
  global $_POST;

  $sql = "update `konferenser` set `datum`=" .sqlvalue(@$_POST["datum"], true).", `streck`=" .sqlvalue(@$_POST["streck"], true).", `datum2`=" .sqlvalue(@$_POST["datum2"], true).", `aktivitet`=" .sqlvalue(@$_POST["aktivitet"], true).", `weblink`=" .sqlvalue(@$_POST["weblink"], true).", `name`=" .sqlvalue(@$_FILES["x_content"]["name"], true).", `type`=" .sqlvalue(@$_FILES["x_content"]["type"], true).", `size`=" .sqlvalue(@$_FILES["x_content"]["size"], true).",".get_file_content_as_sql_for_update("content") ." where " .primarykeycondition();
  mysql_query($sql, $conn) or die(mysql_error());
}

function sql_delete()
{
  global $conn;

  $sql = "delete from `konferenser` where " .primarykeycondition();
  mysql_query($sql, $conn) or die(mysql_error());
}
function primarykeycondition()
{
  global $_POST;
  $pk = "";
  $pk .= "(`id`";
  if (@$_POST["xid"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xid"], false);
  };
  $pk .= ")";
  return $pk;
}
function get_file_content_as_sql($fieldname)
{
  global $_FILES;
  $sql = "";
  if (!empty($_FILES))
    $sql=mysql_escape_string(file_get_contents(@$_FILES['x_'.$fieldname]['tmp_name']));
  if ($sql == '')
    $sql = 'null';
  else
   $sql = "'".$sql."'";
  return $sql;
}

function get_file_content_as_sql_for_update($fieldname)
{
  global $_POST;
  $sql = "`$fieldname` = `$fieldname`";
  if (isset($_POST["a_".$fieldname]))
  {
    switch ($_POST["a_".$fieldname]) {
    //remove
    case "2":
      $sql = "`$fieldname`= null";
  
      break;
    //replace
    case "3":
      $sql = "`$fieldname`=" .get_file_content_as_sql($fieldname);
      break;
    }
  }
  return $sql;
}

?>

 

The problem is in this part I think:

 

function get_file_content_as_sql_for_update($fieldname)
{
  global $_POST;
  [b]$sql = "`$fieldname` = `$fieldname`";[/b]
  if (isset($_POST["a_".$fieldname]))
  {

 

I wish to say keep $fieldname = $fieldname, $name = $name, $type = $type and so on.

 

I have been struggling for days! I'm a newbie but I'm doing this to learn!

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.