Jump to content

Insert not working


emediastudios

Recommended Posts

I have a site that i built at home with php, apache and sql. Everything works fine at home on localhost.

But when i post it online my insert statments dont work, only my delete and update records.

Could this have something to do with the fact that only the insert record files have a seperate process file that the info gets posted to?

Here is one of my insert record files.

It doesnt work online, but at home it does ??? ???

<?php require_once('../Connections/gcproperty.php'); ?>
<?php 

//This is the directory where images will be saved 
$target = "../images/news/"; 
$target = $target . basename( $_FILES['photo']['name']); 

//This gets all the other information from the form 
$headline=$_POST['headline']; 
$link=$_POST['link']; 
$content=$_POST['content']; 
$pic=($_FILES['photo']['name']); 





//Writes the photo to the server 
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
{ 


//Tells you if its all ok 
print "<meta http-equiv=\"refresh\" content=\"0;URL=news_added_successfully.php\">"; 
//Writes the information to the database 
mysql_query("INSERT INTO `news` VALUES ('$headline','$pic','$content','$link')") ; 
} 
else { 

//Gives and error if its not 
echo "Sorry, there was a problem uploading your file."; 
} 
?>

Link to comment
Share on other sites

In my update and delete records there all executed in the same file, with the dreamweaver generated code mixed with some of my own.

In the insert record files, the info is processed through a second file.

Just seems stange that it all works on my pc here at home, but not on the net, i only have to change the connection settings file and thats all done. ??? ???

Link to comment
Share on other sites

You haven't really given us the details of "does not work" so it's hard to move on.

 

If, as you said, one script generates a database not selected error then - regardless of what you think you have on the live site - the solution is obvious.

 

Another possible problem source is that the folder where you're trying to move uploaded images to isn't write-enabled.

 

Without some specific error trapping, echoing of queries, etc. it's all guesswork.

Link to comment
Share on other sites

OK.
This is a update file below and works online.
[code]

<?php require_once('../Connections/gcproperty.php'); ?>
<?php
if (!isset($_SESSION)) {
  session_start();
}
$MM_authorizedUsers = "";
$MM_donotCheckaccess = "true";

// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) { 
  // For security, start by assuming the visitor is NOT authorized. 
  $isValid = False; 

  // When a visitor has logged into this site, the Session variable MM_Username set equal to their username. 
  // Therefore, we know that a user is NOT logged in if that Session variable is blank. 
  if (!empty($UserName)) { 
    // Besides being logged in, you may restrict access to only certain users based on an ID established when they login. 
    // Parse the strings into arrays. 
    $arrUsers = Explode(",", $strUsers); 
    $arrGroups = Explode(",", $strGroups); 
    if (in_array($UserName, $arrUsers)) { 
      $isValid = true; 
    } 
    // Or, you may restrict access to only certain users based on their username. 
    if (in_array($UserGroup, $arrGroups)) { 
      $isValid = true; 
    } 
    if (($strUsers == "") && true) { 
      $isValid = true; 
    } 
  } 
  return $isValid; 
}

$MM_restrictGoTo = "restricted.php";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {   
  $MM_qsChar = "?";
  $MM_referrer = $_SERVER['PHP_SELF'];
  if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
  if (isset($QUERY_STRING) && strlen($QUERY_STRING) > 0) 
  $MM_referrer .= "?" . $QUERY_STRING;
  $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
  header("Location: ". $MM_restrictGoTo); 
  exit;
}
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $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;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "site")) {
  $updateSQL = sprintf("UPDATE site SET about1=%s, about2=%s, news1=%s, news2=%s, welcome1=%s, welcome2=%s, services1=%s, services2=%s, aboutgc=%s, aboutgc2=%s, aboutgclink1=%s, aboutgclink2=%s, aboutgclink3=%s, email=%s WHERE id=%s",
                       GetSQLValueString($_POST['about1'], "text"),
                       GetSQLValueString($_POST['about2'], "text"),
                       GetSQLValueString($_POST['news1'], "text"),
                       GetSQLValueString($_POST['news2'], "text"),
                       GetSQLValueString($_POST['welcome1'], "text"),
                       GetSQLValueString($_POST['welcome2'], "text"),
                       GetSQLValueString($_POST['services1'], "text"),
                       GetSQLValueString($_POST['services2'], "text"),
                       GetSQLValueString($_POST['aboutgc1'], "text"),
                       GetSQLValueString($_POST['aboutgc2'], "text"),
                       GetSQLValueString($_POST['aboutgclink1'], "text"),
                       GetSQLValueString($_POST['aboutgclink2'], "text"),
                       GetSQLValueString($_POST['aboutgclink3'], "text"),
                       GetSQLValueString($_POST['email'], "text"),
                       GetSQLValueString($_POST['id'], "int"));

  mysql_select_db($database_gcproperty, $gcproperty);
  $Result1 = mysql_query($updateSQL, $gcproperty) or die(mysql_error());

  $updateGoTo = "edit_success.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $updateGoTo));
}

mysql_select_db($database_gcproperty, $gcproperty);
$query_Recordset1 = "SELECT * FROM site";
$Recordset1 = mysql_query($query_Recordset1, $gcproperty) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?><!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>Administration Panel</title>

<style type="text/css">
<!--
.style1 {color: #000000}
body {
background-color: #000000;
}
.style2 {
color: #FFFFFF;
font-size: 10px;
}
-->
</style>

<link href="../css/admin.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
.style3 {font-size: 10px}
.style4 {font-size: 16px}
-->
</style>
<link href="../css/main.css" rel="stylesheet" type="text/css" />
</head>


<body><? include("admin_header.php") ?>
<div id="container"><table width="780" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td colspan="2" class="admin"><div align="left">
      <blockquote>
        <p><u><br />
<span class="subtitle">Edit GCProperty.</span></u><br />
          <br />
          <br />
        </p>
      </blockquote>
    </div></td>
  </tr>
  <tr>
    <td colspan="2" background="../grafics/bar_news.png" class="admin"><div align="left"><span class="style7"> Edit Main Contents</span></div></td>
  </tr>
  <tr>
    <td colspan="2" background="../grafics/bar_news.png" class="admin"><form id="site" name="site" method="POST" action="<?php echo $editFormAction; ?>">
      <table width="780" border="0" background="../grafics/bg_big.png">
        <tr>
          <td width="168"><div align="center">About</div></td>
          <td width="602"><label>
            <textarea name="about1" cols="80" rows="5" id="about1"><?php echo $row_Recordset1['about1']; ?></textarea>
          </label></td>
        </tr>
        <tr>
          <td><div align="center">About 2</div></td>
          <td><label>
            <textarea name="about2" cols="80" rows="5" id="about2"><?php echo $row_Recordset1['about2']; ?></textarea>
          </label></td>
        </tr>
        <tr>
          <td><div align="center">Welcome</div></td>
          <td><label>
            <textarea name="welcome1" cols="80" rows="5" id="welcome1"><?php echo $row_Recordset1['welcome1']; ?></textarea>
          </label></td>
        </tr>
        <tr>
          <td><div align="center">Welcome 2</div></td>
          <td><label>
            <textarea name="welcome2" cols="80" rows="5" id="welcome2"><?php echo $row_Recordset1['welcome2']; ?></textarea>
          </label></td>
        </tr>
        <tr>
          <td><div align="center">Services</div></td>
          <td><label>
            <textarea name="services1" cols="80" rows="5" id="services1"><?php echo $row_Recordset1['services1']; ?></textarea>
          </label></td>
        </tr>
        <tr>
          <td><div align="center">Services 2</div></td>
          <td><label>
            <textarea name="services2" cols="80" rows="5" id="services2"><?php echo $row_Recordset1['services2']; ?></textarea>
          </label></td>
        </tr>
        <tr>
          <td><div align="center">About the Gold Coast</div></td>
          <td><label>
            <textarea name="aboutgc1" cols="80" rows="5" id="aboutgc1"><?php echo $row_Recordset1['aboutgc']; ?></textarea>
          </label></td>
        </tr>
        <tr>
          <td><div align="center">About the Gold Coast 2</div></td>
          <td><label>
            <textarea name="aboutgc2" cols="80" rows="5" id="aboutgc2"><?php echo $row_Recordset1['aboutgc2']; ?></textarea>
          </label></td>
        </tr>
        <tr>
          <td><div align="center">Usefull Link 1</div></td>
          <td><label>
            <input name="aboutgclink1" type="text" id="aboutgclink1" value="<?php echo $row_Recordset1['aboutgclink1']; ?>" size="50" />
          </label></td>
        </tr>
        <tr>
          <td><div align="center">Usefull Link 2</div></td>
          <td><label>
            <input name="aboutgclink2" type="text" id="aboutgclink2" value="<?php echo $row_Recordset1['aboutgclink2']; ?>" size="50" />
          </label></td>
        </tr>
        <tr>
          <td><div align="center">Usefull Link 3</div></td>
          <td><label>
            <input name="aboutgclink3" type="text" id="aboutgclink3" value="<?php echo $row_Recordset1['aboutgclink3']; ?>" size="50" />
            <input type="hidden" name="email" id="email" />
          </label></td>
        </tr>
        <tr>
          <td><div align="center"></div></td>
          <td><label>
            <input name="id" type="hidden" id="id" value="<?php echo $row_Recordset1['id']; ?>" size="50" />
            <input type="submit" name="submit" id="submit" value="Submit" />
            <input type="reset" name="Reset" id="Reset" value="Reset" />
          </label></td>
        </tr>
        <tr>
          <td> </td>
          <td> </td>
        </tr>
        <tr>
          <td> </td>
          <td> </td>
        </tr>
        <tr>
          <td><label></label></td>
          <td> </td>
        </tr>
      </table>
        <input type="hidden" name="MM_update" value="site" />
    </form>    </td>
  </tr>
  
</table>
<div align="center"><br />
</div>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>

 

This file does the update process in the same file, as you know.

 

This is my add property file

<?php require_once('../Connections/gcproperty.php'); ?>
<?php
//***FUNCTIONS

//filter extensions
function valid_ext($file_name)
{
$valid = array("jpeg","jpg","png","gif");
$extension = strtolower(substr($file_name,-3,3));
return (in_array($extension, $valid));
}


//filter by size,
function valid_size($size)
{
return ($size <= 1048576);
}


//This is the directory where images will be saved 
$path = '../images/';


//This gets all the other information from the form 
$name=$_POST['name']; 
$rank=$_POST['rank']; 
$suburb=$_POST['suburb']; 
$price=$_POST['price']; 
$content=$_POST['content']; 
$content2=$_POST['content2']; 
$agentmobile=$_POST['agentmobile']; 
$agentemail=$_POST['agentemail']; 
$uploadFile0=($_FILES['uploadFile0']['name']); 
$uploadFile1=($_FILES['uploadFile1']['name']);
$uploadFile2=($_FILES['uploadFile2']['name']); 
$uploadFile3=($_FILES['uploadFile3']['name']);
$uploadFile4=($_FILES['uploadFile4']['name']);
$uploadFile5=($_FILES['uploadFile5']['name']);
$uploadFile6=($_FILES['uploadFile6']['name']);
$uploadFile7=($_FILES['uploadFile7']['name']);
$uploadFile8=($_FILES['uploadFile8']['name']);


// Uploads Images
$uploadNeed = $_POST['uploadNeed'];

// start for loop
$copied = 0;//the number of files successfully uploaded
for($x=0;$x<$uploadNeed;$x++)
{
$file_name = $_FILES['uploadFile'. $x]['name'];

//test
$Size = $_FILES['uploadFile'. $x]['size'];

//Test Check
$Valid = false;
if(valid_ext($file_name))
{
	echo " {$file_name} valid ext";
	$Valid = true;
}else{
	print "<meta http-equiv=\"refresh\" content=\"0;URL=invalid_extension.php\">";
    exit();
}
echo "</br>";
if(valid_size($Size))
{
	echo " {$file_name} valid size</br>";
}else{
	$Valid = false;
	print "<meta http-equiv=\"refresh\" content=\"0;URL=invalid_size.php\">";
	exit(); 
}
if($Valid)
{
	// strip file_name of slashes
	$file_name = stripslashes($file_name);
	$file_name = str_replace("'","",$file_name);
	if(file_exists($path . $file_name) ) {
		print "<meta http-equiv=\"refresh\" content=\"0;URL=file_exists.php\">";
        exit();
	}else {
		$copy = move_uploaded_file($_FILES['uploadFile'. $x]['tmp_name'], $path . $file_name);
		$copied++;//increment our counter
	}	
}
}

if($copied > 0) {

//do your insert stuff

if($copied != $uploadNeed) {

$not_uploaded = $uploadNeed - $copied;

		print "<meta http-equiv=\"refresh\" content=\"0;URL=upload_error.php\">";
        exit();
  }

}
// check if successfully copied

if($copied == $uploadNeed)
{
 	 //Writes the information to the database

mysql_query("INSERT INTO `employees` VALUES (NULL,'$name', '$rank', '$suburb', '$price', '$content', '$content2','$agentmobile', '$agentemail','$uploadFile0','$uploadFile1', '$uploadFile2', '$uploadFile3', '$uploadFile4', '$uploadFile5', '$uploadFile6', '$uploadFile7', '$uploadFile8')")or die (mysql_error());

 print "<meta http-equiv=\"refresh\" content=\"0;URL=property_added_successfully.php\">";



}else{

            echo "<br>$file_name The File(s) could not be uploaded!<br>The file must be under 1 meg and be of a valid extension type, jpeg, jpg, png or gif!<br />
<br />
Please go <a href=\"property_add.php\">back</a> and try again";
}



// end of loop
?>

 

This works on my pc but not when posted online.

I only have two files to change when i post my site online - ('../Connections/gcproperty.php') and check login.php, both these files have been edited to match the username, password and databasename.[/code]

Link to comment
Share on other sites

get this returned error when i do the change to my code below

 

Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /home/emediast/public_html/gcproperty/admin/add_test.php on line 109

No database selected

 

<?php
// check if successfully copied

if($copied == $uploadNeed)
{
 	 //Writes the information to the database
mysql_select_db($database_emediast_gcproperty, $emediast_gcproperty);	 
mysql_query("INSERT INTO `employees` VALUES (NULL,'$name', '$rank', '$suburb', '$price', '$content', '$content2','$agentmobile', '$agentemail','$uploadFile0','$uploadFile1', '$uploadFile2', '$uploadFile3', '$uploadFile4', '$uploadFile5', '$uploadFile6', '$uploadFile7', '$uploadFile8')")or die (mysql_error());

 print "<meta http-equiv=\"refresh\" content=\"0;URL=property_added_successfully.php\">";



}?>

Link to comment
Share on other sites

Oh dear. Unless those variables have already been defined, they have null values and you'll never connect.

 

My preference is to create a file for the connection variables, saved as db_conn.php, thus:

 

<?php
$db_host = "localhost"; // almost certainly OK
$db_name = "xxxxx"; // enter YOUR complete database name
$db_user = "xxxxx"; // enter YOUR complete database user name
$db_pass = "xxxxx"; // enter YOUR complete database user password
?>

 

And then to use it thus:

 

<?php
include("db-conn.php");// or wherever it is relative to this file
mysql_connect($db_host, $db_user, $db_pass) or die ("Error: Unable to connect"); 
mysql_select_db($db_name) or die ("Error: Unable to open the database.");

$db_table = "news"; // your table name here
// Perform MySQL query
$query = "SELECT * FROM $db_table"; 
$result = mysql_query($query) or die("Error ". mysql_error(). " with query ". $query)
?>

 

 

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.