2 Replies - 181 Views - Last Post: 19 April 2013 - 11:35 AM Rate Topic: -----

#1 UnknownCodester  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 05-March 13

Error with mysql_affected_rows

Posted 19 April 2013 - 11:07 AM

Hi, I am basically trying to link my php file to my database but I am having a problem.

I have used mysql_affected_rows to detect if a sql query has been successful but instead of doing that, it seems to print out the statement itself on the webpage.

<html>
<head>
</head>
<body>


<?php
	
// all the data needed, stored in variables, to access the server and database
	$dbHostname = "****"; 
	$dbDatabase = "****";
	$dbUsername = "****";
	$dbPassword = "****";

// use to establish a connection to the MySQL Server, above variables are passed as parameters
	$connection = mysql_connect($dbHostname, $dbUsername ,$dbPassword); 

// if no connection is established produce an error message
	if (!$connection) die("Unable to connect to MySQL : ".mysql_error()); 

// connects to database or produces an error message that database is not selected
	mysql_select_db($dbDatabase) ||die ("Unable to select database : ".mysql_error());

// if the submit button is clicked
	if (isset($_POST["Submit"])) 
	 	{
		// Updates the database with the Users details

		
		$sqlInsert = "INSERT INTO customer (customer_number, customer_name, address_line1, address_line2, address_line3, address_line4, post_code, phone_number) VALUES (DEFAULT,'$_POST[customer_name]','$_POST[address_line1]','$_POST[address_line2]','$_POST[address_line3]','$_POST[address_line4]','$_POST[post_code]','$_POST[phone_number]')";
		mysql_query($sqlInsert);

			if(mysql_affected_rows()>0) // here is where the problem is
			{
			 echo "Victory!";
			}
			else
			{
			 echo "Failure";
			}
		}	 // until here
?>
 
<!-- HTML BELOW FOR THE FORM -->

<!DOCTYPE html>
<h5 alight="right"><a href="login.html">Logout</a></h5>
<h1 align ="center">Customer</h1>
<h2 align ="center"><a href="index.html">Home</a> <a href="jobcard.html">Job Card</a></h2>

<form name ="form1" method="post" action ="*****">
Customer Name: <input type = "text" name = "customer_name" size="50"/><br/>
Address Line 1: <input type = "text" name = "address_line1" size="100"/><br/>
Address Line 2: <input type = "text" name = "address_line2" size="100"/><br/>
Address Line 3: <input type = "text" name = "address_line3" size="100"/><br/>
Address Line 4: <input type = "text" name = "address_line4" size="100"/><br/>
Post Code: <input type = "text" name = "post_code" size="10"/><br/>
Phone Number: <input type = "text" name = "phone_number" size="20"/><br/>
<input type ="submit" name = "Submit" value="Add Customer"/>
</form>
</body>
</html>



So basically instead of being an if statement it prints out "0) { } else { } } ?> " at the top of the webpage!

Posted Image

Is This A Good Question/Topic? 0
  • +

Replies To: Error with mysql_affected_rows

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,738
  • Joined: 08-August 08

Re: Error with mysql_affected_rows

Posted 19 April 2013 - 11:24 AM

Stop using deprecated mysql. Read up on prepared statements. Fix this major problem first!
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2875
  • View blog
  • Posts: 9,543
  • Joined: 12-December 12

Re: Error with mysql_affected_rows

Posted 19 April 2013 - 11:35 AM

To embed PHP array elements in a string you need to enclose them in curly brackets:

{$_POST[customer_name]}

Embedding a constant (DEFAULT) is more problematic; SO topic. It might be easier if you just split out this constant using string concatenation:

..(" . DEFAULT . "..

or use sprintf.

The mysql library is deprecated - see my signature.

This post has been edited by andrewsw: 19 April 2013 - 11:36 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1