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
https://forums.phpfreaks.com/topic/41199-change-code-from-php-5-to-php-4/
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;

}

 

?>

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 );

 

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.