Reputation: 13
I'm having issues running this code here:
<?php
include_once "global.php";
class Project{
//Variable declarations
public $name;
public $pm;
public $members;
public $details;
//Not sure if dates are necessary yet
//$startDate = $inStartDate;
//$endDate = $inEndDate;
//Define the constructor
function __construct($inName, $inPm, $inMembers, $inDetails){
$this->name = $inName;
$this->pm = $inPm;
$this->members = $inMembers;
$this->details = $inDetails;
//Not sure if dates are necessary yet
//$startDate = $inStartDate;
//$endDate = $inEndDate;
}
//Function to create the Project within the database
public function createProject(){
echo "starting";
//Initiate database connection, checking for errors
$con = dbConnect();
if ($con->connect_errno){
echo "Connect failed: %s\n". $mysqli->connect_error;
exit();
}
//Insert the project into the projects database using the information used to instantiate
if ($stmt = $con->prepare("INSERT INTO projects (name, details) VALUES (?,?)")){
echo "Not getting here.";
$stmt->bind_param('ss', $this->name, $this->details);
$stmt->execute();
if (strlen($stmt->error) > 0){
return "Error creating project. Contact system administrator.";
}
elseif(count($members) == 0) {
//ensure there are members to enter
$last_insert = mysqli_insert_id($con);
$con->query('INSERT INTO project_member (pid,uid,rid) VALUES ('.$last_insert.','.$this->pm.',1');
exit();
}
else{
//get last inserted ID
$last_insert = mysqli_insert_id($con);
echo $last_insert;
//loop through members and create sql array
$sql = array();
foreach ($members as $user){
$sql[]='("'.$last_insert.','.$user.',0")';
}
//Add members to join table with roles
$con->query('INSERT INTO project_member (pid,uid,rid) VALUES '.implode(',', $sql));
$con->query('INSERT INTO project_member (pid,uid,rid) VALUES ('.$last_insert.','.$this->pm.',1');
return true;
}
$stmt->close();
}
}
}
It will get to the mysqli code loop, but not initiate it. Trying to echo from it results in nothing being submitted, and the PHP error logs don't note anything going on. I don't see anything strange on the MySQL logs as well.
The $members variable is going to be an array composed of integers, which is why it loops at the bottom through the array.
I am not getting any connection errors, and echoing after trying to capture errors results in success.
dbConnect() is from the global.php file above, and returns a mysqli connection. This is the inside of the connection:
function dbConnect(){
$con = mysqli_connect($_HOST, $_DBUNAME, $_DBPASS,$_DBTABLE);
if ($con->connect_errno){
printf("Connect failed: %s\n", $mysqli->connect_error);
return false;
}
return $con;
}
I have used that elsewhere on my site, and in much of the same situation. Not sure what I am doing wrong here, but any advice would be greatly appreciated.
Edit: I did try again with all of the above suggestions, and still getting the weird issue with it not wanting to go past the if loop.
Edit2: Never mind, I am a moron.
Issue was that the 'details' row in my database didn't exist. Instead, in my infinite wisdom, I called it description and didn't update the code. Changing it fixed the issue.
Again, thank you all for a great first post on StackOverflow. I am going to be back, but I will get better!
Upvotes: 1
Views: 68
Reputation: 38645
The problem starts at your Project constructor
:
The way you're initializing $name, $pm, $members, $details
is keeping them local to the constructor
. You want to initialize these variables using $this
.
//Define the constructor
function __construct($inName, $inPm, $inMembers, $inDetails){
$this->name = $inName;
$this->pm = $inPm;
$this->members = $inMembers;
$this->details = $inDetails;
}
You can then safely get rid of all the global
variables defined within functions, some of them including:
global $name;
global $details;
global $members;
Finally you need to access the member variables name, pm, members, details
and any others using:
$this->name, $this->pm, and so on
Lastly as other answerers have already pointed out on the SQL string generation.
$sql = array();
foreach ($members as $user){
$sql[] = '("'.$last_insert.','.$user.',0")';
}
Upvotes: 1
Reputation: 781096
This loop:
foreach ($members as $user){
$sql='("'.$last_insert.','.$user.',0")';
}
should be:
foreach ($members as $user){
$sql[]='("'.$last_insert.','.$user.',0")';
}
You're just overwriting the variable with a string, not appending to the array.
I'm surprise implode(',', $sql)
on the next line isn't reporting an error.
Upvotes: 2
Reputation: 10732
You initialize $sql
as an array, but then assign a string to it; you need to add items to an array differently:
$sql = array();
foreach ($members as $user){
$sql[] ='("'.$last_insert.','.$user.',0")';
}
Note the square brackets - that adds an extra item to the $sql
array.
I'd also look at the two queries you're running at the end:
$con->query('INSERT INTO project_member (pid,uid,rid) VALUES '.implode(',', $sql));
$con->query('INSERT INTO project_member (pid,uid,rid) VALUES ('.$last_insert.','.$pm.',1');
You're adding two sets of values to the same table - $pm
is never defined. You've got $pm
as a class variable, but you don't set it as such. The syntax for that would be:
function __construct($inName, $inPm, $inMembers, $inDetails){
$this->name = $inName;
$this->pm = $inPm;
$this->members = $inMembers;
$this->details = $inDetails;
That means that the rest of the class functions can access those variables as $this->name
, and so on.
Upvotes: 1