-2

I am a beginner in PHP. I have this function.

function create_group($name, $description, $invites){
global $link;

$name = mysqli_real_escape_string($link, $name);
$description = mysqli_real_escape_string($link, $description);
$names = mysqli_query($link, "SELECT `group_name` FROM `groups` WHERE `group_name` = '$name'");

if(mysqli_num_rows($names) == 0 && mysqli_num_rows($descriptions) == 0){
mysqli_query($link, "INSERT INTO `groups` (`group_name`, `group_description`) VALUES ('$name', '$description')");
}else{
print 'Group with that name/description already exists.';
}
$result = mysqli_query($link, "SELECT `group_id` FROM `groups` WHERE `group_name` = '$name'");
foreach($result as $resul){
foreach($resul as $resu){
$group_id = $resu;
}}}
$invitesarr = explode(',', $invites);
foreach($invites as $invite){
$idres = mysqli_query($link, "SELECT `user_id` FROM `users` WHERE `username` = '$invite'");
if(mysqli_num_rows($idres) == 0){
exit("1 or more of the users that you entered do(es) not exist!");
}else{
foreach($idres as $idarr){
foreach($idarr as $id){
mysqli_query($link, "INSERT INTO `group_members` (`group_id`, `user_id`, `confirmed?`) VALUES ('$group_id', '$id', 0)");
}}}}

$link is an existing database connection. I get the variables for the argument from this page:

<?php
include "function_inc.php";
if(isset($_POST['name'], $_POST['description'], $_POST['invites'])){
create_group($_POST['group_name'], $_POST['description'], $_POST['invites']);
}
?>

<html>
<body>

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

<div>
<label for="group_name">Group Name: </label>
<input type="text" name="group_name" id="group_name" />
</div>

<div>
<label for="invites">Invite...</label>
<input type="text" name="invites" id="invites" />
</div>

<div>
<label for="description">Description: </label>
<textarea name="description" id="description"></textarea>
</div>

<div>
<input type="submit" value="Create" />
</div>

</form>

</body>
</html>

I don't understand why I then get the following messages:

Notice: Undefined variable: invites in /home/peter/Documents/Logarithm/function_inc.php on line 49

Notice: Undefined variable: invites in /home/peter/Documents/Logarithm/function_inc.php on line 50

Please help me; I have been looking at this for hours!

Thanks in advance.

EDIT: Thank you, Benjamin Such! I have now solved the variable message, however I have a new problem: Warning: Invalid argument supplied for foreach(). Any help would be much appreciated.

Petrus
  • 41
  • 7

3 Answers3

2

The $invites variable is only available inside the function. It looks like your may have 1 extra } in your code

$group_id = $resu;
}}}

Which means you function declaration is finished.

Try indenting your code correctly so you can easily see where all of your brackets are.

Personally I would avoid using global variables, I would pass those variables into the function as a dependency.

Also, consider using PDO instead of the mysqli_* functions, as these are a little old school.

The following code isn't right

$invitesarr = explode(',', $invites);
foreach($invites as $invite){

$invites looks like it's a string, and you are trying to iterate through the string rather than through $invitesarr

MajorCaiger
  • 1,893
  • 1
  • 12
  • 18
  • thank you! It got rid of the variable message. However, I now have the following notice: Warning: Invalid argument supplied for foreach() in /home/peter/Documents/Logarithm/function_inc.php on line 53 – Petrus Jul 10 '14 at 21:21
  • Looks like one of your foreach loops isn't getting an array, I don't know which line is line 53, but maybe your result is not an array? May well be false if the query failed. Found it: "$invitesarr = explode(',', $invites); foreach($invites as $invite){" $invites is a string, $invitesarr is the array, your are trying to iterate through the string – MajorCaiger Jul 10 '14 at 21:24
  • it's the foreach($invites as $invite) – Petrus Jul 10 '14 at 21:25
  • Just updated my comment – MajorCaiger Jul 10 '14 at 21:26
  • Thank you very much! You have just solved both of my problems! – Petrus Jul 10 '14 at 21:27
  • I'm sorry I definitely would if I had enough rep... – Petrus Jul 10 '14 at 21:29
2

I would better recommend that you start echo each and every statement one by one and immediately put and exit, so that you can observe which values you really have and where doesn't. that is the best debugging method. For example:

function create_group($name, $description, $invites) {
echo $name;
exit;
.
.
.
}
Kashif Ullah
  • 672
  • 6
  • 19
1

I reorganized the code above:

function create_group($name, $description, $invites) {

  global $link;

  $name = mysqli_real_escape_string($link, $name);
  $description = mysqli_real_escape_string($link, $description);
  $names = mysqli_query($link, "SELECT `group_name` FROM `groups` WHERE `group_name` = '$name'");

  if(mysqli_num_rows($names) == 0 && mysqli_num_rows($descriptions) == 0) {
    mysqli_query($link, "INSERT INTO `groups` (`group_name`, `group_description`) VALUES ('$name', '$description')");
  } else {
    print 'Group with that name/description already exists.';
  }

  $result = mysqli_query($link, "SELECT `group_id` FROM `groups` WHERE `group_name` = '$name'");

  foreach($result as $resul) {
    foreach($resul as $resu) {
      $group_id = $resu;
    }
  }
}
$invitesarr = explode(',', $invites);
foreach($invites as $invite) {
  $idres = mysqli_query($link, "SELECT `user_id` FROM `users` WHERE `username` = '$invite'");
  if(mysqli_num_rows($idres) == 0) {
    exit("1 or more of the users that you entered do(es) not exist!");
  } else {
    foreach($idres as $idarr) {
      foreach($idarr as $id) {
        mysqli_query($link, "INSERT INTO `group_members` (`group_id`, `user_id`, `confirmed?`) VALUES ('$group_id', '$id', 0)");
      }
    }
  }
}

The problem is the foreach($invites as $invite) after the function definition. You want to loop through $invites, but this variable does not exist in the context. $invites exists only as an argument for the function. At least from what we can see from the code you posted us.

Edit: Sorry the error occured on a line before the foreach at $invitesarr = explode(',', $invites);

Fortuna
  • 611
  • 6
  • 18
  • Thank you very much. I have tried your reorganized code, but it does not work. How would you recommend removing the error in the 'explode' line – Petrus Jul 10 '14 at 21:17
  • I told you the reason why it doesn't work. You use `$invites` out of the function context and thats why the error is thrown. You try to acces this variable, although its not defined. – Fortuna Jul 10 '14 at 21:33
  • yes, sorry, I was silly and did not get it the first time. Now I have got it and it was that that fixed my second problem. Thanks! – Petrus Jul 10 '14 at 21:33