Jump to content

Would love some help


sastiger

Recommended Posts

I keep getting this

 

Parse error: syntax error, unexpected T_VARIABLE in C:\xampp\htdocs\home\post.php on line 8

 

Here is my actual code for the post.php page

 

<?php

$loginname = $_POST['loginname'];

$password = $_POST['password'];

 

mysql_connect ("localhost", "root", "majick") or die (

mysql_select_db ("animal1")

 

$result = mysql_query("INSERT INTO login ($username, $password)

VALUES ('$username', $password')");

 

mysql_query($query) or die (’Error updating database’);

 

echo “database was updated with: ” ".$username.” “.$password.”;

 

?>

 

Please help me any help would be greatly appreciated!

Link to comment
Share on other sites

You're missing a ; after mysql_select_db().

 

Also, you're having a missing ' in your mysql query around $password

 

And this:

echo “database was updated with: ” ".$username.” “.$password.”;

 

should be

echo "database was updated with: $username $password";

Link to comment
Share on other sites

Still am getting the same error here is my page which is posting to the php file

 

<html>

<head>

<title>Pet creation page</title>

</head>

<body>

<form method="post" action="post.php">

<td>Pet name:</td>

<input type="text" name="$petname" value="" size="12" />

<br>

<td>Type:</td>

<input type="text" name="$pettype" value="" size="12" />

<br>

<td>Price:</td>

<input type="text" name="$petprice" value="" size="12" />

<br>

<input type="submit" value="Update Database" />

</form>

</body>

</html>

 

and here is my php post page

 

<?php

 

$petname = $_POST['name'];

$pettype = $_POST['type'];

$petprice = $_POST['250'];

 

$host="localhost";

$user="root";

$pass="majick";

 

mysql_connect ("$host", "$user", "$pass") or die (

mysql_select_db(pet)

 

$result = mysql_query("INSERT INTO animal (name, type, price)

VALUES ('$petname', '$pettype', '$petprice');

 

mysql_query or die (’Error updating database’);

 

?>

 

PLEASE HELP  ;D

Link to comment
Share on other sites

$petname = $_POST['name'];
$pettype = $_POST['type'];
$petprice = $_POST['petprice'];

$host="localhost";
$user="root";
$pass="majick";

mysql_connect ($host, $user, $pass) or die ('some error message');
mysql_select_db("pet");

$result = mysql_query("INSERT INTO animal (name, type, price)
VALUES ('$petname', '$pettype', '$petprice')");

mysql_query or die ('Error updating database');

Link to comment
Share on other sites

$query = "INSERT INTO animal (name, type, price) VALUES ('$petname', '$pettype', '$petprice')";

$result = mysql_query($query) or die (mysql_error);

 

 

Replace the last 2 lines in the example AndyB posted and see if you get any errors. When troubleshooting query errors, the best thing to do is echo the query as it is set in the script and look for errors there. If that don't work, copy/paste into phpmyadmin and see what you get there. I have saved hours of headache by doing this. A lot of times phpmyadmin will tell you what is wrong and you can modify and tweak the query there until it is right. Then you can port it back to your script.

 

I found a database class script that I use exclusively for all my DB work. I have modified it so I can place it in the root of the server, and call it from several sites and connect to multiple databases with it. Makes this type of stuff MUCH simpler.

 

I will share it if you would like.

 

Nate

 

Link to comment
Share on other sites

Should be:

$query = "INSERT INTO animal (name, type, price) VALUES ('$petname', '$pettype', '$petprice')";
$result = mysql_query($query) or die mysql_error();

 

die is a built in function. So it is correct as die(). I believe that die without the parentheses is correct as well, but the php manual shows it in use as die()

 

But, you did bring an error to my attention. It should be die(mysql_error()).

 

I had an extra space in there and forgot the parentheses for the mysql_error function.

 

Nate

Link to comment
Share on other sites

ahhh whatever... it appears in the php manual under Misc Functions.. and is referenced as function in many places including w3schools.com so in my book it is a function.

 

On php.net it says

This language construct is equivalent to exit().
in the description under Misc Functions.

 

So either way is right. Function, Language construct whatever. :)

 

Nate

Link to comment
Share on other sites

I agree. For everyday use there's no difference.

 

Edit:

 

Except for this note

 

    Note: Because this is a language construct and not a function, it cannot be called using variable functions

 

But I think it's not in the scope of 'everyday use' for most people.

Link to comment
Share on other sites

Read up on mysql and how to use it.... but here is the database class.

 

<?php
class cDatabase {

  //class variables defined in constructor
  var $host;
  var $user;
  var $password;
  var $database;
  var $type;
  
  //constructor - needed for connection string
  function cDatabase($type)
  {
  	if($type == 'db1')
  	{  
  		 $hostName='HOST1';
	 $userName='USER1';
	 $passwordName='PASSWORD1';
	 $databaseName='DB NAME1';
}
elseif($type == 'db2')
{ 
	$hostName='HOST2';
	$userName='USER2';
	$passwordName='PASSWORD2';
	$databaseName='DB NAME 2';
}
else
{
	$error = 'Valid DB could not be selected';
}

  
    $this->host = $hostName;
    $this->user = $userName;
    $this->password = $passwordName;
    $this->database = $databaseName;

}

  //loop through paired arrays buildng an sql INSERT statement
  function sqlInsert($dataNames, $dataValues, $tableName){
  $sqlNames = "INSERT INTO " . $tableName . "(";
  for($x = 0; $x < count($dataNames); $x++) 
  {
	  if($x != (count($dataNames) - 1)) 
	  {
		 $sqlNames = $sqlNames . $dataNames[$x] . ", ";
		 @$sqlValues = $sqlValues . "'" . addslashes($dataValues[$x]) . "', ";
	  }
	  else 
	  {
		  $sqlNames = $sqlNames . $dataNames[$x] . ") VALUES(";
		  $sqlValues = $sqlValues . "'" . $dataValues[$x] . "')";
	  }
  }
  
  	$result = $this->ExecuteNonQuery($sqlNames . $sqlValues);
   	$results['inserted'] = 0;
	$results['skipped'] = 0;
	$results['last_id'] = mysql_insert_id();

   if($result == 1)
   {
   		$results['inserted'] = 1;
   }
   else
   {
   		$results['skipped'] = 1;
   }
   
   return $results;
  
  
  }

  //loop through paired arrays buildng an sql UPDATE statement
  function sqlUpdate($dataNames, $dataValues, $tableName, $condition){
  $sql = "UPDATE " . $tableName . " SET ";
  for($x = 0; $x < count($dataNames); $x++) 
  {
	  if($x != (count($dataNames) - 1)) 
	  {
		  $sql = $sql . $dataNames[$x] . "= '" . $dataValues[$x] . "', ";
	  }
	  else 
	  {
		  $sql = $sql . $dataNames[$x] . "= '" . $dataValues[$x] . "' ";
	  }
  }
  $sql = $sql . $condition;

    $result = $this->ExecuteNonQuery($sql);
	$results['inserted'] = 0;
	$results['skipped'] = 0;
	$results['last_id'] = mysql_insert_id();
  
   if($result == 1)
   {
   		$results['inserted'] = 1;
   }
   else
   {
   		$results['skipped'] = 1;
   }
   
   return $results;
  }

    //execute a query
  function ExecuteNonQuery($sql)
  {
	$conn = mysql_connect($this->host, $this->user, $this->password);
	$sql = mysql_real_escape_string($sql);
	mysql_select_db ($this->database) ;
	$result = mysql_query($sql,$conn) ;
	$changed = mysql_affected_rows();
  
return $changed;

  }
  
  //execute a query and return a recordset
  function ExecuteReader($query)
  {
    $conn = mysql_connect($this->host, $this->user, $this->password);
$query = mysql_real_escape_string($query);
    mysql_select_db ($this->database);
$result = mysql_query($query,$conn);
return $result;

  } 

}
?>

 

To use it, you instantiate the class like this.

<?php
$db = new cDatabase('db1'); // this will connect to the database defined in db1 
?>

 

Then to use it's functions you do it like so..

 

Select Query:

<?php
$result = $db->ExecuteReader("SELECT * FROM tableName WHERE this='$that'");
echo mysql_num_rows($result);
?>

 

Insert Query:

<?php
$names = array('field1','field2','field3','field4');
$values = array($val1, $val2, $val3, $val4);
$result = $db->sqlInsert($names, $values, 'tableName');
echo $result['inserted']; // will return 1 or 0
?>

 

Update is the same as insert but you use this instead.

<?php
$result = $db->sqlUpdate($names, $values, 'tableName', $whereClause);
?>

 

Delete Query:

<?php
$result = $db->ExecuteNonQuery("DELETE FROM tablename WHERE this='$that'");
?>

 

You can set this up to connect to multiple databases by adding additional $type sections.

 

<?php
        if($type == 'db1')
  	{  
  		 $hostName='HOST1';
	 $userName='USER1';
	 $passwordName='PASSWORD1';
	 $databaseName='DB NAME1';
}
?>

 

It may even be better to make this a switch instead of a bunch of elseif. This is by no means perfect as a db class, but it works. I have tried to optimize it and sanitize all db input in it.

 

 

Nate

Link to comment
Share on other sites

Why do you start a new connection each query?  That's absolutely horrible.  Also, with the constructor, you should just have all of the details passed in as an array so you can store it in like $this->conn_info, then use a private _connect() method to get the proper connection.  If they enter the wrong db details, sucks for them.  You'd throw an Exception, not make a $error.

Link to comment
Share on other sites

DarkWater,

 

I did not write this. I started to write my own db class, but found this one and just have not taken too much time in making it better.

 

I am also not real versed on oop and classes. This was a quick and simple method of a universal db class that I found. I will take your advice under consideration and do some research at some point and try to make it better.

 

Thanks for the tips.

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.