Jump to content

Change code from PHP 5 to PHP 4


byronbailey

Recommended Posts

Hi

 

I am new to PHP, i have copied code from a book that includes my_sqli which i have found that it is not compatible with the PHP 4 that my web hosts uses.

 

Can any help make it compatible with PHP4?

 

Would be great for of any help, with the two bits of code below.

 

Thanks

Nick

<?php

define('DB_USER', 'nbailey') ;
define('DB_PASSWORD', 'nbailey');
define('DB_HOST', 'localhost');
define('DB_NAME', 'proploc_com_-_seller'); 

$dbc = mysqli_connect(DB_HOST, DB_USER, DB_USER, DB_NAME) OR die ('Could not connect to MySQL: ' .mysqli_connect_error());

function escape_data($data){

if(ini_get('magic_quotes_gpc')) {
	$data = stripslashes($data);
}
global $dbc;

$data = mysqli_real_escape_string($dbc, trim($data));
return $data;
}

?>

<html>

<head>

<title>Add a print</title>

</head>

<body>

<?php
require_once('mysql_connect.php');


if(isset($_POST['submitted'])){


//Check for a print name
if(!empty($_POST['print_name'])){
$pn = escape_data($_POST['print_name']);
} else {
$pn = FALSE;
echo '<p><font color="red">Please enter the print\'s name!</font></p>';
}


//Check for an image
if(is_uploaded_file($_FILES['image']['tmp_name'])){
if(move_uploaded_file($_FILES['image']['tmp_name'],"uploads/{
$_FILES[image][name]}")){
	echo'<p>The file has been uploaded!</p>';
} else {
	echo '<p><font color="red">The file could not be moved</font></p>';
	$i = FALSE;
}
$i = $_FILES['image']['name'];
} else {
	$i = FALSE;
}


//Check for a size
if(!empty($_POST['size'])){
	$s = escape_data($_POST['size']);
} else {
	$s = '<i>Size information not available.</i>';
}



//Check for a price
if(is_numeric($_POST['price'])){
	$p = (float) $_POST['price'];
} else {
	$p = FALSE;
	echo '<p><font color="red">Please enter the print\'s price!</font></p>';
}


//Check for a description
if(!empty($_POST['descriptions'])){
	$d = escape_data($_POST['description']);
} else {
$d = '<i>No Descriptions available.</i>';
}


if($_POST['artist'] == 'new'){


$query = 'INSERT INTO artists (first_name, middle_name,last_name) VALUES (';


	if(!empty($_POST['first_name'])){
		$query .="'".escape_data($_POST['first_name'])."',";
	} else {
		$query .='NULL, ';
	}


	if(!empty($_POST['middle_name'])){
		$query .="'".escape_data($_POST['middle_name'])."',";
	} else {
		$query .='NULL, ';
	}



	if(!empty($_POST['last_name'])){
		$query .= "'".escape_data($_POST['last name'])."')";
	$result = mysqli_query($dbc, $query);
	$a = mysqli_insert_id;
	} else {
		$a = FALSE;
	echo '<p><font color="red">Please enter the artist\'s name!</font></p>';
	}


} 
elseif (($_POST['artist']=='existing') && ($_POST['existing'] > 0)) {
	$a = (int) $_POST['existing'];
} 

else {
	$a = FALSE;
echo '<p><font color="red">Please enter or select the print\'s artist!</font></p>';
}



if($pn && $p && $a && $i){

	$query = "INSERT INTO prints(artist_id, print_name, price, size, description, image_name) VALUES ($a, $pn, $p, $s, $d, $i)";

	if($result = mysqli_query ($dbc, $query)) {
		echo '<p>The print has been added</p>';
	}

	else {
		echo '<p><font color="red">Your submission could not be processed due to a system error.</font></p>';
	}
}

else {
	echo '<p><font color="red">Please click back and try again.</font.</p>'; 

}

}
else {

?>


<form enctype="multipart/form-data" action="add_print.php" method="post">

<input type="hidden" name="MAX_FILE_SIZE" value="524288"/>

<fieldset><legend>Fill out the form to add a print to the catalog:</legend>

<p><b>Print Name:</b> <input type="text" name="print_name" size="30" maxlength ="60" /></p>

<p><b>Image:</b> <input type="file" name="image" /> <small>The file name should not include
spaces or other invalid characters and should have a file extension.</small></P>

<p><b>Artists:</b><input type="radio" name="artist" value="existing" /> Existing =></p>
<select name="existing">
<option>Select One</option>

<?php
/*			$query = "SELECT artist_id, CONCAT_WS(' ', first_name, middle_name, last_name) AS name
	FROM artists ORDER By last_name, first_name ASC";
	$result = mysqli_query($dbc, $query);
	while ($row = mysql_fetch_array ($result, MYSQLI_ASSOC)){

	echo "<option value=\"{$row['artist_id']}\">{$row['name']}</option>\n";

	}

mysqli_close($dbc); */

?> 

</select></p>

<p>
<input type="radio" name="artist" value="new" /> New =>
First Name: <input type="text" name="first_name" size="10" maxlength="20"/>
Middle Name: <input type="text" name="middle_name" size="10" maxlength="20"/>
Last Name: <input type="text" name="last_name" size="10" maxlength="30"/>
</p>

<p><b>Price:</b><input type="text" name="price" size="10" />Do not include the dollar sign or comma.</p>
<p><b>Size:</b><input type="text" name="size" size="10" maxlength /></p>
<p><b>Description:</b><textarea name="description" cols="40" row="5"></textarea><p>

</fieldset>

<div align="center"><input type="submit" name="submit" value="submit" /></div>
<input type="hidden" name="submitted" value="TRUE" />

</form>

<?php

}

?>

</body>

</html>

 

 

Link to comment
Share on other sites

Just a tip. No on likes reading through a hundred lines of code to find your problem and then fix it. You will more likely get an answer if you find a few lines that do not work and post those. Once you post those someone will help you.

Link to comment
Share on other sites

This is the first time I have posted on a Forum, so thanks for the advice. 

 

I have used mysqli to connect to mysql.

 

$dbc = mysqli_connect(DB_HOST, DB_USER, DB_USER, DB_NAME) OR die ('Could not connect to MySQL: ' .mysqli_connect_error());

 

function escape_data($data){

 

if(ini_get('magic_quotes_gpc')) {

$data = stripslashes($data);

}

global $dbc;

 

$data = mysqli_real_escape_string($dbc, trim($data));

return $data;

}

 

?>

Link to comment
Share on other sites

An exception to the above is mysql_connect() which doesn't have the DB_NAME argument. You need a separate mysql_select_db() statement.

 

$dbc = mysql_connect(DB_HOST, DB_USER, DB_USER) OR die ('Could not connect to MySQL: ' .mysql_error());

 

mysql_select_db ( DB_NAME, $dbc );

 

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.