Using php variables in sql from different php blocks (Undefined variable: xxxx) -
i following this tutorial on how modify sql database. seems fine in when run code below error saying undefined index in line 11 , 12 not defined. can point mistake? can use variable 1 block in another?(the guy in tutorial does)
<?php include '/connection.php'; if(!isset($_post['submit'])){ $query="select * shop id=$_get[id]"; $result=mysqli_query($conn,$query)or die(mysqli_error($conn)); $shop=mysqli_fetch_array($result); } ?> <form action="modify.php" method="post"> <input name="name" value="<?php echo $shop['name']; ?>"> //error here <input name="city" value="<?php echo $shop['city']; ?>"> //and here <input type="hidden" name="id" value="<?php echo $_get['id']; ?>"> <input type="submit" name=submit value="modify"> </form> <?php if(isset($_post['submit'])){ $q1="update shop set name='$_post[name]',city='$_post[city]' id=$_post[id]"; mysqli_query($conn,$q1)or die(mysqli_error($conn)); } ?>
try read , use parametrized queries (pdi or mysqli) these vulnerable sql injection.
you need define $shop=array("shop"=>"","city"=>""); on top of page:
<?php include '/connection.php'; $shop=array("shop"=>"","city"=>""); if(!isset($_post['submit'])){ $query="select * shop id=$_get[id]"; $result=mysqli_query($conn,$query)or die(mysqli_error($conn)); $shop=mysqli_fetch_array($result); } ?> <form action="modify.php" method="post"> <input name="name" value="<?php echo $shop['name']; ?>"> //error here <input name="city" value="<?php echo $shop['city']; ?>"> //and here <input type="hidden" name="id" value="<?php echo $_get['id']; ?>"> <input type="submit" name=submit value="modify"> </form> <?php if(isset($_post['submit'])){ $q1="update shop set name='$_post[name]',city='$_post[city]' id=$_post[id]"; mysqli_query($conn,$q1)or die(mysqli_error($conn)); } ?>
or can put isset this:
<?php include '/connection.php'; if(!isset($_post['submit'])){ $query="select * shop id=$_get[id]"; $result=mysqli_query($conn,$query)or die(mysqli_error($conn)); $shop=mysqli_fetch_array($result); } ?> <form action="modify.php" method="post"> <input name="name" value="<?php if(isset($shop['name'])) echo $shop['name']; ?>"> //error here <input name="city" value="<?php if(isset($shop['city'])) echo $shop['city']; ?>"> //and here <input type="hidden" name="id" value="<?php echo $_get['id']; ?>"> <input type="submit" name=submit value="modify"> </form> <?php if(isset($_post['submit'])){ $q1="update shop set name='$_post[name]',city='$_post[city]' id=$_post[id]"; mysqli_query($conn,$q1)or die(mysqli_error($conn)); } ?>
Comments
Post a Comment