4 Replies - 428 Views - Last Post: 15 May 2013 - 01:42 AM Rate Topic: -----

#1 Dice93  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 39
  • Joined: 24-December 12

syntax error

Posted 15 May 2013 - 12:59 AM

Hi there,

I can't find where I made a mistake, for years...

$row = "INSERT INTO Student (Name, Surename, StudentID, Generation) VALUES ('$_POST[$student_data[0]]', '$_POST[$student_data[1]]', '$_POST[$student_data[2]]', '$_POST[$student_data[3]]')";


This is report: Parse error: syntax error, unexpected '[', expecting ']' in


Thanks in advance...

Is This A Good Question/Topic? 0
  • +

Replies To: syntax error

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3480
  • View blog
  • Posts: 10,032
  • Joined: 08-June 10

Re: syntax error

Posted 15 May 2013 - 01:07 AM

arrays inside strings need to be treated slightly differently than usual. the problem resolves itself if you do something against SQL Injection (which you are currently wide open to).

using Prepared Statements, it could look like
// using PDO as example
$ps = $pdo->prepare("INSERT INTO Student (Name, Surename, StudentID, Generation) VALUES (:first, :last, :id, :gen)");
$ps->bindValue("first", $_POST[$student_data[0]], PDO::PARAM_STR);
$ps->bindValue("last",  $_POST[$student_data[1]], PDO::PARAM_STR);
$ps->bindValue("id",    $_POST[$student_data[2]], PDO::PARAM_INT); // assuming student id is an integer
$ps->bindValue("gen",   $_POST[$student_data[3]], PDO::PARAM_STR);
$ps->execute();

Was This Post Helpful? 0
  • +
  • -

#3 Dice93  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 39
  • Joined: 24-December 12

Re: syntax error

Posted 15 May 2013 - 01:31 AM

Look, this is a homework assignment, I know this is a bad practice, but atm I don't really care much about learning php, since I'm more focused on Java and this teacher is not giving detailed instructions about php.

I'm going to show you now what I've done so far, and if you could please write me how this can be more elegant written,
because I really don't have much time left.. and this is getting really frustrating because I do not know the syntax..

	$student_data = array('Name', 'Surename', 'StudentID', 'Generation');
	foreach ($student_data as $data) {
		if (empty($_POST[$data])) {
			header('Location: output.php?fail');
		} else {
			$sql = true;
		}
	}
	
	if ($sql) {
		$row = "INSERT INTO Student (Name, Surename, StudentID, Generation) VALUES ('$_POST[$student_data[0]]', '$_POST[$student_data[1]]', '$_POST[$student_data[2]]', '$_POST[$student_data[3]]')";
		
		if ($exc) {
			// to do success page
		} else {
			// to do 
		}
	}



Or could you actually tell me how can I write code like I mention below, in case where I have to store this values from form to db.
Something like for that col get value from array[0], array[1].. with for loop?

This post has been edited by Dice93: 15 May 2013 - 01:28 AM

Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3480
  • View blog
  • Posts: 10,032
  • Joined: 08-June 10

Re: syntax error

Posted 15 May 2013 - 01:38 AM

View PostDice93, on 15 May 2013 - 10:28 AM, said:

atm I don't really care much about learning php, since I'm more focused on Java and this teacher is not giving detailed instructions about php.

I believe that SQL Injection would be an issue in Java as well (if you have to put user input into a DB).

View PostDice93, on 15 May 2013 - 10:28 AM, said:

I'm going to show you now what I've done so far, and if you could please write me how this can be more elegant written,

the most elegant solution (IMHO) I lined out in my previous post. however, if you donít give a **** about PHP, you can at least use sprintf().

View PostDice93, on 15 May 2013 - 10:28 AM, said:

and this is getting really frustrating because I do not know the syntax..

you can always look up the syntax in the PHP Manual, there are lots of examples, too.

one more comment: if you set a Location header, it is good practice to kill the script with an exit call, as there should be no reason to process the remainder of the script.
Was This Post Helpful? 0
  • +
  • -

#5 Dice93  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 39
  • Joined: 24-December 12

Re: syntax error

Posted 15 May 2013 - 01:42 AM

if you donít give a **** about PHP


That answer...
I said I do not care at the moment about it, not that I do not give a shit.

Thanks for that kind a help.
Was This Post Helpful? -3
  • +
  • -

Page 1 of 1