Copy link to clipboard
Copied
Hello,
I have read here on the forum how to upload an image to server and store path in your database, the image uploads correctly to the correct folder on my server but the image path does not get stored on my sql database (not local hosting). I receive the error: The file has been uploaded, and your information has been added to the directory. Column 'image' cannot be null.
My database has the following columns:
id | datum | image | sectie |
---|
My code is as follows:
<?php require_once('Connections/dbTroch.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
mysql_select_db($database_dbTroch, $dbTroch);
$query_rs_aanbod = "SELECT * FROM tblSlideshow ORDER BY id ASC";
$rs_aanbod = mysql_query($query_rs_aanbod, $dbTroch) or die(mysql_error());
$row_rs_aanbod = mysql_fetch_assoc($rs_aanbod);
$totalRows_rs_aanbod = mysql_num_rows($rs_aanbod);
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
$target = "images/slides/"; //This is the directory where images will be saved//
$target = $target . basename( $_FILES['image']['name']); //change the image and name to whatever your database fields are called//
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "add-photos-aanbod")) {
$insertSQL = sprintf("INSERT INTO tblSlideshow (image, sectie) VALUES (%s, %s)",
GetSQLValueString($_POST['file'], "text"),
GetSQLValueString($_FILES['image']['name'], "text"));
//This code writes the photo to the server//
if(move_uploaded_file($_FILES['image']['tmp_name'], $target))
{
//And confirms it has worked//
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {
//Gives error if not correct//
echo "Sorry, there was a problem uploading your file.";
}
mysql_select_db($database_dbTroch, $dbTroch);
$Result1 = mysql_query($insertSQL, $dbTroch) or die(mysql_error());
}
?>
<!doctype html>
<html>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Troch Project Solutions - Admin - Toevoegen</title>
<link rel="stylesheet" href="css/foundation.css" />
<link rel="stylesheet" href="css/layout.css" />
<!-- Fonts
================================================== -->
<script type="text/javascript" src="//use.typekit.net/vob8gxg.js"></script>
<script type="text/javascript">try{Typekit.load();}catch(e){}</script>
<!-- jQuery
================================================== -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="js/vendor/modernizr.js"></script>
</head>
<body>
<div class="row">
<div class="large-8 medium-8 small-8 large-centered medium-centered small-centered columns intro">
<h2 class="subheader text-center">Admin</h2>
<p><a>Log uit</a></p>
<p><a href="admin.php">Terug naar Admin menu</a>
<h4>image toevoegen naar aanbod slideshows:</h4>
<form action="<?php echo $row_rs_aanbod['']; ?>" method="POST" name="add-photos-aanbod" id="add-photos-aanbod" enctype="multipart/form-data">
<table>
<tbody>
<tr>
<td><label for="image">Kies foto:</label></td>
<td><input name="image" type="file" id="image" value="<?php echo $row_rs_aanbod['image']; ?>" /></td>
<tr>
<td>Sectie:</td>
<td><select name="sectie" id="sectie" option value="sectie">
<?php
do {
?>
<option value="<?php echo $row_rs_aanbod['sectie']?>"><?php echo $row_rs_aanbod['sectie']?></option>
<?php
} while ($row_rs_aanbod = mysql_fetch_assoc($rs_aanbod));
$rows = mysql_num_rows($rs_aanbod);
if($rows > 0) {
mysql_data_seek($rs_aanbod, 0);
$row_rs_aanbod = mysql_fetch_assoc($rs_aanbod);
}
?>
</select></td>
</tr>
<tr>
<td><input type="Submit" name="Add" id="add" value="Toevoegen" /></td>
</tr>
</tbody>
</table>
<input type="hidden" name="MM_insert" value="add-photos-aanbod" />
</form>
</div><!-- end large-8 -->
</div><!-- end row -->
<script src="js/vendor/jquery.js"></script>
<script src="/js/vendor/fastclick.js"></script>
<script src="js/foundation.min.js"></script>
<script>
$(document).foundation();
</script>
</body>
</html>
<?php
mysql_free_result($rs_aanbod);
?>
I cannot work out what is wrong and I would appreciate any help on this. Thanks
Copy link to clipboard
Copied
Your form field and array variable names do not match
<td><input name="image" type="file" id="image" value="<?php echo $row_rs_aanbod['image']; ?>" /></td>
GetSQLValueString($_POST['file'], "text"),